我正在使用Hibernate和JPA。我有一个实体Customer,它引用了ParentCustomer:
Customer
ParentCustomer
public class Customer { @Id @GeneratedValue @Column(name = "CustomerID") private int id; @ManyToOne @JoinColumn(name = "ParentCustomerID") private Customer parent; // ... }
但是在我的数据库中,有些客户没有父母,因此将ParentCustomerID设置为0。我在测试班级时遇到的异常是:
ParentCustomerID
0
javax.persistence.EntityNotFoundException: Unable to find it.keyforup.pat.data.entities.Customer with id 0
有没有一种方法来设置ParentCustomer,以null当id为0?
null
尝试这个
@ManyToOne @JoinColumn(name = "ParentCustomerID") @NotFound(action = NotFoundAction.IGNORE) private Customer parent;