Why does Hibernate EntityManager.find () throw an EntityNotFoundException?

JPA 2.0 javadoc (and probably the spec) say EntityManager.find () returns null when the entity is not found:

instance of the found entity or null if the entity does not exist

However, the Hibernate impl instead throws an EntityNotFoundException. What for? Observed using Hibernate 3.6.8 and hibernate-jpa-2.0-api v1.0.1.Final

javax.persistence.EntityNotFoundException: Unable to find bar.foo.Entity with identifier 144487 at org.hibernate.ejb.Ejb3Configuration $ Ejb3EntityNotFoundDelegate.handleEntityNotFound (Ejb3Configuration.java:137reventLearTreventErutentreventrevent .java: 320) at org.hibernate.event.def.DefaultLoadEventListener.onLoad (DefaultLoadEventListener.java:152) on org.hibern. SessionImpl.fireLoad (SessionImpl.java:1090) at org.hibernate.impl.SessionImpl.get (SessionImpl.java:1005) at org.hibernate.impl.SessionImpl.get (SessionImpl.java:998) at org.hibernate.ejb .AbstractEntityManagerImpl.find (AbstractEntityManagerImpl.java:779) at org.hibernate.ejb.AbstractEntityManagerImpl.find (AbstractEntityManagerImpl.java:754)

+4
source share
1 answer

I had the same problem using Hibernate 4.1.x and I wanted to point a pointer to someone experiencing the same problem. In my case, as mentioned in previous comments, this is not the entity itself, but rather another entity referenced by the loaded object. The strange thing was that this reference object did exist, but it was accessed in another transaction / session, and that mattered. Since I wrote a test using spring, simply changing the @transactional move from class level to method completely broke everything.

+4
source

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


All Articles