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)
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).
CodePudding user response:
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.