Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null object if entity not found

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?

like image 892
davioooh Avatar asked Apr 27 '12 08:04

davioooh


1 Answers

Try this

@ManyToOne
@JoinColumn(name = "ParentCustomerID")
@NotFound(action = NotFoundAction.IGNORE)
private Customer parent;
like image 180
Bitmap Avatar answered Oct 14 '22 20:10

Bitmap