Zero object if the object is not found

I work with Hibernate and JPA. I have an object called Customer that references ParentCustomer :

 public class Customer { @Id @GeneratedValue @Column(name = "CustomerID") private int id; @ManyToOne @JoinColumn(name = "ParentCustomerID") private Customer parent; // ... } 

But there are some clients in my db that don't have a parent, so the ParentCustomerID parameter ParentCustomerID set to 0 . The exception that I get when testing my class is:

javax.persistence.EntityNotFoundException: Unable to find it.keyforup.pat.data.entities.Customer with id 0

Is there a way to set ParentCustomer to null when id is 0 ?

+6
source share
1 answer

try it

 @ManyToOne @JoinColumn(name = "ParentCustomerID") @NotFound(action = NotFoundAction.IGNORE) private Customer parent; 
+19
source

Source: https://habr.com/ru/post/914307/


All Articles