How solve javax.persistence.EntityNotFoundException with JPA (not by using @NotFound)

后端 未结 6 831
悲哀的现实
悲哀的现实 2021-02-05 10:28

We are using JPA to load some stuff from a database. Some entities may have optional relationships between them, e.g.

@Entity
public class First {
    ....
    @         


        
6条回答
  •  悲&欢浪女
    2021-02-05 10:40

    Below is an alternative solution for this problem. I had to build on top of an old database where the relations sometimes were corrupt. This is how I solved it by only using JPA.

    @PostLoad
    public void postLoad(){
        try {
            if(getObject() != null && getObject().getId() == 0){
                setObject(null);
            }
        }
        catch (EntityNotFoundException e){
            setObject(null);
        }
    } 
    

提交回复
热议问题