1

首先为这么冗长的问题道歉。:)

我有一个类Parent和一个类Child,其中 Parent 到 Child 是OneToMany映射并且ParentChild. 结构如下:

@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)并尝试打印实体对象时,都会收到“断言失败:空标识符”。但是,当我第一次加载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访问父对象都尚未加载。

我的观察对吗?

我该如何解决这个问题?

4

0 回答 0