如果实体未找到,则为空对象 [英] Null object if entity not found
问题描述
我正在使用Hibernate和JPA。我有一个名为 Customer
的实体,它引用了 ParentCustomer
:
public class Customer {
@Id
@GeneratedValue
@Column(name =CustomerID)
private int id;
@ManyToOne
@JoinColumn(name =ParentCustomerID)
私人客户父母;
// ...
}
但是在我的db有一些客户没有父母,所以 ParentCustomerID
被设置为 0
。我测试我的类时得到的异常是:
$ b
javax.persistence.EntityNotFoundException:无法找到它.keyforup.pat.data.entities .Customer with id 0
有没有办法将 ParentCustomer
设置为 null
当id是 0
?
@ManyToOne
@JoinColumn(name =ParentCustomerID)
@NotFound(action = NotFoundAction.IGNORE)
私人客户父母;
I'm working with Hibernate and JPA. I have an entity called Customer
that references a ParentCustomer
:
public class Customer {
@Id
@GeneratedValue
@Column(name = "CustomerID")
private int id;
@ManyToOne
@JoinColumn(name = "ParentCustomerID")
private Customer parent;
// ...
}
But in my db there are some customers that have no parent so the ParentCustomerID
is set to 0
. The exception I get when I test my class is:
javax.persistence.EntityNotFoundException: Unable to find it.keyforup.pat.data.entities.Customer with id 0
Is there a way to set the ParentCustomer
to null
when id is 0
?
Try this
@ManyToOne
@JoinColumn(name = "ParentCustomerID")
@NotFound(action = NotFoundAction.IGNORE)
private Customer parent;
这篇关于如果实体未找到,则为空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!