首先为这样一个冗长的问题道歉。 :)
我有一个班级Parent
和一个班级Child
,其中Parent to Child是OneToMany
映射,并且Parent
中有Child
的引用。结构如下:
@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() + "]");
}
}
我遇到了这个问题。每当我使用Child
加载session.load(Child.class, 10L)
对象并尝试打印实体对象时,我会得到一个&#34;断言失败:null标识符&#34;。但是,当我第一次加载Parent
个对象然后加载Child
对象时,它工作正常。
失败的情况
Child child = (Child) session.load(Child.class, 10L);
System.out.println(child);
有效的方案
List<Parent> parents = (List<Parent>) session.createQuery("from Parent").list();
Child child = (Child) session.load(Child.class, 10L);
System.out.println(child);
异常堆栈跟踪是:
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)
当我尝试自己分析问题时,我发现问题是因为在失败情况下,每当访问child.parent
时父对象尚未加载。
我的观察是对的吗?
如何解决此问题?