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

后端 未结 6 830
悲哀的现实
悲哀的现实 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:39

    I've met the same problem. It's not always reproducable, so I cannot test it, but here are some thoughts:

    1. The instance of your Second class is deleted, while the instance of the First class does not know anything about that.
    2. You need a way to let an instance of First know, when its instance of Second is deleted.
    3. cascade option for removing does not help here.
    4. You may try to use bidirectional relationship, when instance of First exists inside instance of Second. It let you update instance of First via instance of Second before removing second
    5. Bidirectional relationship - is evil. I would suppose in your case, that First - is owner of Second. Don't allow any service delete your Second instance directly. Let service which works with instances of First remove instance of Second. In this case you may make the "second" field nullable first, than remove instance of Second via EntityManager.
    6. You may get exception when execute queries and the 2nd level cache is enabled and query has a hint, which allows to cache its result. I would offer you get result of queries via the following method:
    private List<?> getQueryResult(final Query query)
    {
        try
        {
            return query.getResultList();
        }
        catch (EntityNotFoundException e)
        {
            return query.setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH).getResultList();
        }
    }
    
    1. If you work with entities via EntityManger, but not via queries, and you get exception because entity is cached, you may invalidate all entities of First in cache when you delete Second.

    I'd like to discuss this solution, as I cannot test it and cannot make sute it works. If somebody tries, please let me know.

    PS: if somebody has a unit test for hibernate, that reproduces this issue, could you please let me know. I wish to investigate it further.

    0 讨论(0)
  • 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);
        }
    } 
    
    0 讨论(0)
  • 2021-02-05 10:46

    Try to add the parameter optional = true to your @OneToOne annotation.

    0 讨论(0)
  • 2021-02-05 10:47

    Try to use cascade=CascadeType.ALL in @OneToMay(******).

    0 讨论(0)
  • 2021-02-05 10:53

    It happens when you delete the associated entity id. In my case I had Product table depending upon Brand table. I deleted a row or an entity of brand id to which one of the product instance was depending upon.

    0 讨论(0)
  • 2021-02-05 10:58

    What about adding a test in the correspondent entity class:

    public boolean getHasSecond() {
        if (this.Second != null) {
            return true;
        } else {
            return false;
        }
    }
    

    Like this, you can check if the relation exists...

    0 讨论(0)
提交回复
热议问题