我有两个实体DealEntity
和DealTypeEntity
他们是相关的 -
@Entity
class DealEntity{
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "DEAL_TYPE_ID")
private DealTypeEntity dealTypeEntity;
}
我正在尝试通过创建包含DealTypeEntity ref。
的DealEntity实例进行保存我正在使用JPA,它在entityManager.persist(entity)
Oct 17, 2013 3:36:34 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [/Travel-Portal] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException:org.hibernate.TransientPropertyV alueException: object references an unsaved transient instance - save the transient instance before flushing: training.impetus.tp.model.DealEntity.dealTypeEntity -> training.impetus.tp.model.DealTypeEntity; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing: training.impetus.tp.model.DealEntity.dealTypeEntity -> training.impetus.tp.model.DealTypeEntity] with root cause
org.hibernate.TransientPropertyValueException: object references an unsaved transient i nstance - save the transient instance before flushing: training.impetus.tp.model.DealEntity.dealTypeEntity -> training.impetus.tp.model.DealTypeEntity
at org.hibernate.engine.spi.CascadingAction$8.noCascade(CascadingAction.java:380)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:176)
at o rg.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEv entListener.java:160)
at o rg.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlus hingEventListener.java:151)
at o rg.hiborg.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:151)
at org.hib
答案 0 :(得分:14)
要么您执行错误消息建议“在刷新之前保存瞬态实例”
entityManager.persist(dealTypeEntity);
entityManager.persist(dealEntity);
或者您可以将DealEntity中的注释更改为
@ManyToOne(fetch = FetchType.EAGER, cascade=CascadeType.PERSIST)
// then persisting DealEntity would persist the childs also
entityManager.persist(dealEntity);
答案 1 :(得分:0)
还有另一种情况我们可以得到同样的错误,
当我们手动插入update_count列为空值的条目时,我们遇到了同样的问题。所以我们有一个与基金表相关的成员表。我们手动插入了一个新基金,忘记为update_count设置一个值。当应用程序使用此基金创建成员时,我们在保存成员数据org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing:
时遇到此异常。我们使用update_count列进行版本控制[@version]。
答案 2 :(得分:-1)
而不是传递参考对象传递保存的对象,下面是解释我的问题的解释,
//错误
entityManager.persist(dealTypeEntity);
dealEntity.setDealType(dealTypeEntity);
entityManager.persist(dealEntity)
//右
DealTypeEntity savedEntity = entityManager.persist(dealTypeEntity);
dealEntity.setDealType(savedEntity);
entityManager.persist(dealEntity)