我收到一个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;
}
并在EntityB中:
@OneToOne(fetch = FetchType.LAZY, mappedBy = "entityB")
public EntityA getEntityA() {
return this.entityA;
}
因此从EntityA到EntityB有一个CascadeType.ALL。我已经检查过我不是直接持久化EntityB,我打印出日志,告诉我只有我正在查看的两个实体是持久的,而entityA实例指向的是指向正确的entityA实例的entityB实例(和我可以从堆栈跟踪中看到持久性来自级联)。因此,entityB因为entityA的级联而持久存在,现在entityB抱怨entityA是短暂的???
任何想法可能会出错?