org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: com.ldb.mdac.PhysicalTableRole

Hibernate JIRA | Ravi Kumar | 2 years ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    Lets say there are two POJO entities defined 'DataModelGroup' and 'PhysicalTableRole'. The entities are annotated with OneToOne bidirectional mapping. According to the OneToOne mapping model, at any point, there should not be more than one PhysicalTableRole assigned to a DataModelGroup and vice-versa. On doing few inserts into the db, figured out that hibernate allows to insert more than one row (pointing to the same DataModelGroup) in PhysicalTableRole which basically violates OneToOne mapping between the objects. Following is the scenario used to reproduced the issue: 1) Open a session. 2) Create a DataModelGroup record (say dmg) into the db. 3) Create a PhysicalTableRole (say ptr1) record and set dmg to it. Insert ptr1 into the db. 4) Close session 5) Begin a new session 6) Create another PhysicalTableRole (say ptr2) record and set dmg to it. Insert ptr2 into the db. After the insertion, there are two rows in table PhysicalTableRole pointing to the same DataModelGroup dmg. 7) Fetch the DataModelGroup record by its id. An exception is thrown by the hibernate : Exception in thread "main" org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: com.ldb.mdac.PhysicalTableRole at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86) at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:143) at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2046) at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:775) at org.hibernate.type.EntityType.resolve(EntityType.java:496) at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:153) at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:127) at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.performTwoPhaseLoad(AbstractRowReader.java:227) at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:198) at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:123) at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121) at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:85) at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167) at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3890) at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:486) at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:451) at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:196) at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:258) at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:134) at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1071) at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:164) at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2623) at org.hibernate.internal.SessionImpl.get(SessionImpl.java:955) at com.ldb.mdac.App.main(App.java:42) I am also attaching a sample hibernate application to reproduce the error.

    Hibernate JIRA | 2 years ago | Ravi Kumar
    org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: com.ldb.mdac.PhysicalTableRole
  2. 0

    Lets say there are two POJO entities defined 'DataModelGroup' and 'PhysicalTableRole'. The entities are annotated with OneToOne bidirectional mapping. According to the OneToOne mapping model, at any point, there should not be more than one PhysicalTableRole assigned to a DataModelGroup and vice-versa. On doing few inserts into the db, figured out that hibernate allows to insert more than one row (pointing to the same DataModelGroup) in PhysicalTableRole which basically violates OneToOne mapping between the objects. Following is the scenario used to reproduced the issue: 1) Open a session. 2) Create a DataModelGroup record (say dmg) into the db. 3) Create a PhysicalTableRole (say ptr1) record and set dmg to it. Insert ptr1 into the db. 4) Close session 5) Begin a new session 6) Create another PhysicalTableRole (say ptr2) record and set dmg to it. Insert ptr2 into the db. After the insertion, there are two rows in table PhysicalTableRole pointing to the same DataModelGroup dmg. 7) Fetch the DataModelGroup record by its id. An exception is thrown by the hibernate : Exception in thread "main" org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: com.ldb.mdac.PhysicalTableRole at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86) at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:143) at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2046) at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:775) at org.hibernate.type.EntityType.resolve(EntityType.java:496) at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:153) at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:127) at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.performTwoPhaseLoad(AbstractRowReader.java:227) at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:198) at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:123) at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121) at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:85) at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167) at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3890) at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:486) at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:451) at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:196) at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:258) at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:134) at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1071) at org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:164) at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2623) at org.hibernate.internal.SessionImpl.get(SessionImpl.java:955) at com.ldb.mdac.App.main(App.java:42) I am also attaching a sample hibernate application to reproduce the error.

    Hibernate JIRA | 2 years ago | Ravi Kumar
    org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: com.ldb.mdac.PhysicalTableRole

    Root Cause Analysis

    1. org.hibernate.HibernateException

      More than one row with the given identifier was found: 1, for class: com.ldb.mdac.PhysicalTableRole

      at org.hibernate.loader.entity.AbstractEntityLoader.load()
    2. Hibernate
      TwoPhaseLoad.initializeEntity
      1. org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:86)
      2. org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:143)
      3. org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2046)
      4. org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:775)
      5. org.hibernate.type.EntityType.resolve(EntityType.java:496)
      6. org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:153)
      7. org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:127)
      7 frames
    3. org.hibernate.loader
      AbstractLoadPlanBasedEntityLoader.load
      1. org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.performTwoPhaseLoad(AbstractRowReader.java:227)
      2. org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:198)
      3. org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:123)
      4. org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121)
      5. org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:85)
      6. org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)
      6 frames
    4. Hibernate
      SessionImpl.get
      1. org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3890)
      2. org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:486)
      3. org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:451)
      4. org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:196)
      5. org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:258)
      6. org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:134)
      7. org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1071)
      8. org.hibernate.internal.SessionImpl.access$2600(SessionImpl.java:164)
      9. org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2623)
      10. org.hibernate.internal.SessionImpl.get(SessionImpl.java:955)
      10 frames
    5. com.ldb.mdac
      App.main
      1. com.ldb.mdac.App.main(App.java:42)
      1 frame