我得到一个 TransientObjectException
org.hibernate.TransientObjectException: object references an unsaved transient instance
- save the transient instance before flushing: EntityB.entityA -> EntityA
at org.hibernate.engine.CascadingAction$9.noCascade(CascadingAction.java:376)
at org.hibernate.engine.Cascade.cascade(Cascade.java:162)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:454)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:49)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:154)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:110)
at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:671)
at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:663)
at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:346)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:291)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:239)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:192)
at org.hibernate.engine.Cascade.cascade(Cascade.java:153)
这两个实体具有 OneToOne 关系:在 EntityA
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "ENTITY_B_ID")
public EntityB getEntityB() {
return this.entityB;
}
在实体 B 中:
@OneToOne(fetch = FetchType.LAZY, mappedBy = "entityB")
public EntityA getEntityA() {
return this.entityA;
}
所以从EntityA到EntityB有一个CascadeType.ALL。我已经检查过我没有直接持久化 EntityB,我打印出日志,这些日志显示只有我正在查看的两个实体被持久化,并且 entityA 实例指向 entityB 实例,该实例指向正确的 entityA 实例(并且我可以从堆栈跟踪中看到持久性来自级联)。所以 entityB 由于来自 entityA 的级联而被持久化,现在 entityB 抱怨 entityA 是瞬态的???
有什么想法可以在这里出错吗?