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

Why we are breaking JPA specification here?

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:

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

@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.

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