3

我有两个实体DealEntityDealTypeEntity 它们是相关的——

@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
4

3 回答 3

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);
于 2013-10-17T11:18:27.517 回答
0

在另一种情况下,我们会遇到同样的错误,当我们手动插入一个 update_count 列为空值的条目时,我们遇到了同样的问题。所以我们有一个与基金表相关的成员表。我们手动插入了一个新基金,但忘记为 update_count 输入一个值。当应用程序使用此基金创建会员时,我们在保存会员数据时遇到了此异常org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing:。我们使用 update_count 列进行版本控制 [@version]。

于 2015-08-26T07:46:17.890 回答
-1

而不是传递参考对象传递了保存的对象,下面是解决我的问题的解释,

//错误的

entityManager.persist(dealTypeEntity);

dealEntity.setDealType(dealTypeEntity);

entityManager.persist(dealEntity)

//正确的

DealTypeEntity savedEntity= entityManager.persist(dealTypeEntity);

dealEntity.setDealType(savedEntity);

entityManager.persist(dealEntity)

于 2016-03-01T10:42:15.643 回答