NotNull annotation preventing builds in spring boot application

Advertisements

I am trying to run a spring boot app.
I have a model class(Category) and a service class(CategoryService)
corresponding to the model

Category.java

@Data
@Entity
public class Category implements Serializable {

    @Id
    @GeneratedValue
    private Integer id;

    @NotNull
    private String categoryName;

    @NotNull
    private Integer totalResourceCount;

    @NotNull
    private Integer categoryValue;

    @Column(updatable = false, nullable = false)
    private Timestamp createdDate;

    @NotNull
    private Timestamp updatedDate;
}

CategoryService.java

@Service
public class CategoryService {

    @Autowired
    CategoryRepository categoryRepository;

    public List<CategoryDTO> getAllCategories() {
        List<CategoryDTO> categoryDTOList = new ArrayList<>();
        List<Category> categories = categoryRepository.findAll(Sort.by("createdDate"));
        if (categories != null) {
            categories.forEach(category -> {
                CategoryDTO categoryDTO = new CategoryDTO();
                categoryDTO.setCategoryId(category.getId());
                categoryDTO.setCategoryName(category.getCategoryName());
                categoryDTO.setTotalResourceCount(category.getTotalResourceCount());
                categoryDTO.setCategoryValue(category.getCategoryValue());
                categoryDTOList.add(categoryDTO);
            });
        }
        return categoryDTOList;
    }

    public void createCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setCategoryName(categoryDTO.getCategoryName());
        category.setCategoryValue(categoryDTO.getCategoryValue());
        category.setTotalResourceCount(0);
        category.setCreatedDate(DateUtil.getCurrentTimestamp());
        category.setUpdatedDate(DateUtil.getCurrentTimestamp());
        categoryRepository.save(category);
    }
}

When I try to build the project, I am getting the following erorr

CategoryService.java:[37,29] constructor Category in class Category cannot be applied to given types;
[ERROR]   required: java.lang.String
[ERROR]   found:    no arguments
[ERROR]   reason: actual and formal argument lists differ in length

I understand that the error is due to instantiating the Category class without passing the @NotNull fields in the constructor. So I removed the @NotNull annotations and the build succeeds. However, this is not desired as the @NotNull annotation have some connection to the field validations.

What is the correct way to solve this issue other than removing the @NotNull annotation?
Do I need to declare a constructor class in the Category model and then initialise the Category instance by passing all the field values that are @NotNull ?

>Solution :

@Data includes @RequiredArgsConstructor, so a constructor will be generated with all @NotNull annotated properties.

If you also want to use a no-args constructor, you have to add @NoArgsConstructor

@Data
@NoArgsConstructor
@Entity
public class Category implements Serializable {

    @Id
    @GeneratedValue
    private Integer id;

    @NotNull
    private String categoryName;

    @NotNull
    private Integer totalResourceCount;

    @NotNull
    private Integer categoryValue;

    @Column(updatable = false, nullable = false)
    private Timestamp createdDate;

    @NotNull
    private Timestamp updatedDate;
}

Now the generate code includes both constructors

Leave a ReplyCancel reply