使用现有但已分离的OneToOne实体的新实体上的em.persist问题

时间:2012-08-26 20:10:52

标签: hibernate jpa persistence

我在JBoss AS 7.1上使用Hibernate,在创建新实体并将现有实体设置为ManyToOne关系时遇到问题。在使用以下代码之前查询现有实体(客户):

TypedQuery<Customer> query = em.createNamedQuery("Customer.findByLastNameLike", Customer.class);
query.setParameter("lastName", "%"+lastName+"%");
return query.getResultList();

em.persist(order)设置order以便customer订购后,当我在新实体order.setCustomer(customer)上执行Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: erp.model.persons.Customer at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141) at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:717) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:710) at org.hibernate.engine.spi.EJB3CascadingAction$1.cascade(EJB3CascadingAction.java:49) at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208) at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165) at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:437) at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:266) at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:138) at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78) at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151) at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78) at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:725) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:701) at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:705) at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:853) 时,会抛出以下异常:

@Entity
public class Order extends AbstractEntity implements Serializable {

    private static final long serialVersionUID = -8061887078955032972L;

    @ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST}, optional = false)
    private Customer customer = null;

@Entity
@DiscriminatorValue("customer")
public class Customer extends Person implements Serializable {

    private static final long serialVersionUID = 2788107135840578322L;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
    private List<Order> orders = null;

实体定义如下:

{{1}}

这种情况有什么问题?

感谢任何暗示......

1 个答案:

答案 0 :(得分:1)

以下答案也回答了我的问题。即使对于新实体,我也从使用em.persist()更改为em.merge()。现在我的整个对象图是成功持久的,无论是否有新的实体混合在一起。

JPA cascade persist and references to detached entities throws PersistentObjectException. Why?

此外,我删除了CascadeType.PERSIST