javax.persistence.EntityNotFoundException: Unable to find test.AddressType with id A

Hibernate JIRA | Todd Tidwell | 8 years ago
  1. 0

    I have an interesting mapping case that is causing me some very odd low-level errors. In this particular case, I'm working with a table that contains multiple objects and I can use a DiscriminatorColumn to differentiate. However, the "primary key" for the child objects is not guaranteed to be uniqe table-wide. For example, DiscriminatorColumn value '013' may have a unique-key value of 'A', DiscriminatorColumn '014' may have the same unique-key value. To map this, I created a base class, gave it the DiscriminatorColumn annotation, and then extended it with my various types. Here are the classes: @Entity @Table(name="WTTBLE") @DiscriminatorColumn(name="PTBLE") public abstract class MetaData { @Id @Basic(fetch=FetchType.EAGER) @Column(name="PCODE", nullable=false, insertable=false, updatable=false) private String code = null; @Basic(fetch=FetchType.EAGER) @Column(name="DEF", nullable=false, insertable=false, updatable=false) private String value = null; .... } @Entity @DiscriminatorValue(value = "015") public class AddressType extends MetaData { // No fields here, they're all mapped in MetaData } @Entity @DiscriminatorValue(value = "016") public class CustomerType extends MetaData { // No fields here, they're all mapped in MetaData } Now, this works great until they both have the same value for the "code" property. At that point, I get the following logging and exception: org.hibernate.impl.SessionImpl - initializing proxy: [com.wiley.permissions.domain.persistence.wintouch.metadata.AddressType#A] .... org.hibernate.event.def.DefaultLoadEventListener - load request found matching entity in context, but the matched entity was of an inconsistent return type; returning null .... javax.persistence.EntityNotFoundException: Unable to find test.AddressType with id A at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:107) at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111) at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150) at test.AddressType$$EnhancerByCGLIB$$16d4c983.getValue(<generated>) After some serious research, I found that org.hibernate.engine.StatefulPersistenceContext is returning the wrong object from it's getEntity method. It's returning a CustomerType object, which was added to the session's cache after the address type. On furthur examination, this seems to be because org.hibernate.engine.EntityKey.equals() is returning the same value for both objects. EntityKey's code for lines 95 -99 is: public boolean equals(Object other) { EntityKey otherKey = (EntityKey) other; return otherKey.rootEntityName.equals(this.rootEntityName) && identifierType.isEqual(otherKey.identifier, this.identifier, entityMode, factory); } Examination of the values of EntityKey for my objects show that rootEntityName for both objects is test.MetaData. That means that this method is going to return true. That, in turn, means that StatefulPersistenceContext's entitiesByKey HashMap is going to return the first one that equals the one passed into the get() method.

    Hibernate JIRA | 8 years ago | Todd Tidwell
    javax.persistence.EntityNotFoundException: Unable to find test.AddressType with id A
  2. 0

    I have an interesting mapping case that is causing me some very odd low-level errors. In this particular case, I'm working with a table that contains multiple objects and I can use a DiscriminatorColumn to differentiate. However, the "primary key" for the child objects is not guaranteed to be uniqe table-wide. For example, DiscriminatorColumn value '013' may have a unique-key value of 'A', DiscriminatorColumn '014' may have the same unique-key value. To map this, I created a base class, gave it the DiscriminatorColumn annotation, and then extended it with my various types. Here are the classes: @Entity @Table(name="WTTBLE") @DiscriminatorColumn(name="PTBLE") public abstract class MetaData { @Id @Basic(fetch=FetchType.EAGER) @Column(name="PCODE", nullable=false, insertable=false, updatable=false) private String code = null; @Basic(fetch=FetchType.EAGER) @Column(name="DEF", nullable=false, insertable=false, updatable=false) private String value = null; .... } @Entity @DiscriminatorValue(value = "015") public class AddressType extends MetaData { // No fields here, they're all mapped in MetaData } @Entity @DiscriminatorValue(value = "016") public class CustomerType extends MetaData { // No fields here, they're all mapped in MetaData } Now, this works great until they both have the same value for the "code" property. At that point, I get the following logging and exception: org.hibernate.impl.SessionImpl - initializing proxy: [com.wiley.permissions.domain.persistence.wintouch.metadata.AddressType#A] .... org.hibernate.event.def.DefaultLoadEventListener - load request found matching entity in context, but the matched entity was of an inconsistent return type; returning null .... javax.persistence.EntityNotFoundException: Unable to find test.AddressType with id A at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:107) at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111) at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150) at test.AddressType$$EnhancerByCGLIB$$16d4c983.getValue(<generated>) After some serious research, I found that org.hibernate.engine.StatefulPersistenceContext is returning the wrong object from it's getEntity method. It's returning a CustomerType object, which was added to the session's cache after the address type. On furthur examination, this seems to be because org.hibernate.engine.EntityKey.equals() is returning the same value for both objects. EntityKey's code for lines 95 -99 is: public boolean equals(Object other) { EntityKey otherKey = (EntityKey) other; return otherKey.rootEntityName.equals(this.rootEntityName) && identifierType.isEqual(otherKey.identifier, this.identifier, entityMode, factory); } Examination of the values of EntityKey for my objects show that rootEntityName for both objects is test.MetaData. That means that this method is going to return true. That, in turn, means that StatefulPersistenceContext's entitiesByKey HashMap is going to return the first one that equals the one passed into the get() method.

    Hibernate JIRA | 8 years ago | Todd Tidwell
    javax.persistence.EntityNotFoundException: Unable to find test.AddressType with id A
  3. 0

    @ManyToOne of the same type as parent where child doesn't exist in DB

    Stack Overflow | 3 years ago | Jan Vladimir Mostert
    javax.persistence.EntityNotFoundException: Unable to find Transaction with id 0
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    EntityNotFoundException in Hibernate Many To One mapping however data exist

    Stack Overflow | 4 years ago | Badal Singh
    javax.persistence.EntityNotFoundException: Unable to find com.indianretailshop.domain.User with id 5
  6. 0

    [EJB-260] OneToMany relationship not persiting merges - Hibernate JIRA

    atlassian.net | 1 year ago
    javax.persistence.EntityNotFoundException: Unable to find Customer with id CIDD803997

    2 unregistered visitors
    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. javax.persistence.EntityNotFoundException

      Unable to find test.AddressType with id A

      at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound()
    2. Hibernate EJB
      Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound
      1. org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:107)
      1 frame
    3. Hibernate
      AbstractLazyInitializer.getImplementation
      1. org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79)
      2. org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68)
      3. org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
      3 frames
    4. Hibernate
      CGLIBLazyInitializer.invoke
      1. org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
      1 frame
    5. test
      AddressType$$EnhancerByCGLIB$$16d4c983.getValue
      1. test.AddressType$$EnhancerByCGLIB$$16d4c983.getValue(<generated>)
      1 frame