Hibernate Parent-Child One-To-Many Relationship -
first of apologies such verbose question. :)
i have class parent
, class child
, parent child onetomany
mapping , there reference parent
in child
. structure 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 having issue this. whenever load child
object using session.load(child.class, 10l)
, try print entity object, "assertion failure: null identifier". however, when first load parent
objects , load child
object, working fine.
scenario fails
child child = (child) session.load(child.class, 10l); system.out.println(child);
scenario 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 @ org.hibernate.engine.spi.entitykey.<init>(entitykey.java:69) @ org.hibernate.internal.abstractsessionimpl.generateentitykey(abstractsessionimpl.java:241) @ org.hibernate.loader.loader.extractkeysfromresultset(loader.java:722) @ org.hibernate.loader.loader.getrowfromresultset(loader.java:635) @ org.hibernate.loader.loader.doquery(loader.java:850) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:289) @ org.hibernate.loader.loader.doqueryandinitializenonlazycollections(loader.java:259) @ org.hibernate.loader.loader.loadentity(loader.java:2042) @ org.hibernate.loader.entity.abstractentityloader.load(abstractentityloader.java:82) @ org.hibernate.loader.entity.abstractentityloader.load(abstractentityloader.java:72) @ org.hibernate.persister.entity.abstractentitypersister.load(abstractentitypersister.java:3710) @ org.hibernate.event.internal.defaultloadeventlistener.loadfromdatasource(defaultloadeventlistener.java:439) @ org.hibernate.event.internal.defaultloadeventlistener.doload(defaultloadeventlistener.java:420) @ org.hibernate.event.internal.defaultloadeventlistener.load(defaultloadeventlistener.java:204) @ org.hibernate.event.internal.defaultloadeventlistener.onload(defaultloadeventlistener.java:143) @ org.hibernate.internal.sessionimpl.fireload(sessionimpl.java:992) @ org.hibernate.internal.sessionimpl.immediateload(sessionimpl.java:907) @ org.hibernate.proxy.abstractlazyinitializer.initialize(abstractlazyinitializer.java:158) @ org.hibernate.proxy.abstractlazyinitializer.getimplementation(abstractlazyinitializer.java:195) @ org.hibernate.proxy.pojo.javassist.javassistlazyinitializer.invoke(javassistlazyinitializer.java:185) @ parent_$$_javassist_63.getparentid(parent_$$_javassist_63.java) @ child.tostring(child.java:110)
when tried analyze issue myself, observed issue because in failure scenario, whenever child.parent
being accessed parent object not yet loaded.
is observation right?
how solve issue?
Comments
Post a Comment