Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

First of all apologies for such a verbose question. :)

I have a class Parent and a class Child, where Parent to Child is OneToMany mapping and there is a reference to Parent in the Child. The structure is as follows:

@Entity
@Table (name = "PARENT_TABLE")
class Parent {
   @Id
   @Column (name = "PARENT_ID")
   protected Long parentId;

   @Column (name = "PARENT_NM")
   protected String parentName;

   @OneToMany (mappedBy = "parent")
   @Column (name = "PARENT_ID")
   protected Set<Child> children;

}

@Entity
@Table (name = "CHILD_TABLE")
class Child {
   @Id
   @Column (name = "CHILD_ID")
   protected Long childId;

   @Column (name = "CHILD_NM")
   protected String childName;

   @ManyToOne
   @JoinColumn (name = "PARENT_ID", referencedColumnName = "PARENT_ID")
   protected Parent parent;

   public String toString() {
      return "Child [childId=" + childId + ", parentId=" + parent.getParentId() + "]");
   }
}

I am having an issue with this. Whenever I load a Child object using session.load(Child.class, 10L) and try to print the entity object, I get an "Assertion Failure: null Identifier". However, when I first load the Parent objects and then load the Child object, it is working fine.

Scenario that fails

Child child = (Child) session.load(Child.class, 10L);
System.out.println(child);

Scenario that works

List<Parent> parents = (List<Parent>) session.createQuery("from Parent").list();
Child child = (Child) session.load(Child.class, 10L);
System.out.println(child);

The exception stacktrace is:

Exception in thread "main" org.hibernate.AssertionFailure: null identifier
    at org.hibernate.engine.spi.EntityKey.<init>(EntityKey.java:69)
    at org.hibernate.internal.AbstractSessionImpl.generateEntityKey(AbstractSessionImpl.java:241)
    at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:722)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:635)
    at org.hibernate.loader.Loader.doQuery(Loader.java:850)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.loadEntity(Loader.java:2042)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3710)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:439)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:420)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:143)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:992)
    at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:907)
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:158)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
    at Parent_$$_javassist_63.getParentId(Parent_$$_javassist_63.java)
    at Child.toString(Child.java:110)

When I tried to analyze the issue myself, I observed that the issue is because in the failure scenario, whenever child.parent is being accessed the parent object is not yet loaded.

Is my observation right?

How do I solve this issue?

share|improve this question
    
I think referencedColumnName is not required when you reference to the primary key. It may cause the problem ... –  Stefan Steinegger Apr 5 '13 at 9:59
    
Thanks for your solution! However, even this doesn't work! :( –  Shyam Apr 5 '13 at 11:47
    
Make the the set inverse. –  Stefan Steinegger Apr 5 '13 at 12:11
    
(Which is a good idea anyway, but most probably doesn't solve the problem ...) –  Stefan Steinegger Apr 5 '13 at 12:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.