Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How should I fix "Expected 0 arguments but found 3" error if I want to use Lombok RequiredArgsConstructor annotation?

I am using Spring, JPA, Java17, MySQL.
IDE: IntelliJ IDEA 2022.2.4
JDK: Amazon Coretto 17.0.6

I am getting an error "Expected 0 arguments but found 3". (image)

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my Article entity class code and I am using Lombok to remove boilerplate code. For some reason RequiredArgsConstructor annotation cannot be well managed in test class and I need to create actual constructor to be able to work on it.

@Entity
@Getter
@Setter
@RequiredArgsConstructor
@Table(name = "article", schema = "chitchat")
public class Article {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "title", nullable = false, length = 150)
    private String title;

    @OneToOne
    @JoinColumn(name = "category_id")
    private Category category;

    @Column(name = "comment_count", nullable = false)
    private int commentCount;

    @Column(name = "view_count", nullable = false)
    private int viewCount;

    @ToString.Exclude
    @OneToMany(mappedBy = "article", orphanRemoval = true)
    private Set<Tag> tags = new LinkedHashSet<>();

    @Column(name = "modification_date")
    private LocalDateTime modificationDate;

    @Column(name = "creation_date", nullable = false)
    private LocalDateTime creationDate;

    @Column(name = "content", nullable = false, length = 50000)
    private String content;

    @OneToOne(optional = false, orphanRemoval = true)
    @JoinColumn(name = "author_id", nullable = false)
    private User author;

    @Column(name = "published", nullable = false)
    private Boolean published = false;

    @OneToMany(mappedBy = "article")
    private Set<Comment> comments = new LinkedHashSet<>();
}

I tried using AllArgsConstructor and creating constructor by hand (works fine).

>Solution :

As docs of @RequiredArgsConstructor suggests:

Generates a constructor with required arguments. Required arguments
are final fields and fields with constraints such as @NonNull.

So, your class does not have fields that match those criteria

Whereas @AllArgsConstructor :

Generates an all-args constructor. An all-args constructor requires
one argument for every field in the class.

So, everything works as expected.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading