Issue
I use Lombok and JPA in my program and I intend to create an Owner object with builder annotation, but in the BaseEntity file when I put @AllArgsConstructor on the class, IntelliJ gives me this warning and when I add @NoArgsConstructor The warning goes away:
Using @Builder/@AllArgsConstructor for JPA entities without defined no-argument constructor breaks JPA specification.
BaseEntity:
@Getter
@Setter
@AllArgsConstructor
@MappedSuperclass
@NoArgsConstructor
public class BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
Person:
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@MappedSuperclass
public class Person extends BaseEntity {
private String firstName;
private String lastName;
public Person(Long id,String firstName, String lastName) {
super(id);
this.firstName = firstName;
this.lastName = lastName;
}
}
Owner:
@Getter
@Setter
@ToString
@NoArgsConstructor
@Entity
@AttributeOverride(name = "id", column = @Column(name = "owner_id"))
public class Owner extends Person {
@Builder
public Owner(Long id,String firstName, String lastName, Set<Pet> pets, String address, String city, String telephone) {
super(firstName, lastName);
this.pets = pets;
this.address = address;
this.city = city;
this.telephone = telephone;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
@ToString.Exclude
private Set<Pet> pets = new HashSet<>();
private String address, city, telephone;
}
My question is why does the jpa specification break?
Solution
A JPA Entity requires a no-arg constructor by specification.
Answered By - Mihe
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)