简体   繁体   中英

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:

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

A JPA Entity requires a no-arg constructor by specification .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM