gpt4 book ai didi

java - 在与非托管对象具有持久关联的 jpa 实体 : You cannot flush unmanaged objects. 中设置外键时出错

转载 作者:太空宇宙 更新时间:2023-11-04 07:15:17 25 4
gpt4 key购买 nike

我正在尝试使用 JPA 2.0 设置一些指向静态查找表的外键链接。包含链接和静态查找表的实体分别在类 A 和状态中定义如下。我还有一个名为 AHistory 的实体,它在所有方面都是 A 的克隆。当我调用 logNewA 方法时,我从 em 中找到已经存在的实体状态,创建一个新的 A 实体,然后创建一个相应的 AHistory 实体,如下所示。当我尝试执行此操作时,我收到此错误。谁能帮帮我吗?

@Entity
@Table(name = "ATABLE")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class A extends BaseBusinessObject {

@Id
@Column(name = "AID"
private long id;

@JoinColumn(name = "STATUSID")
@ManyToOne
private Status status;

// other irrelevant fields in BaseBusinessObject class.
}

public class AHistory extends BaseBusinessObject {

@Id
@Column(name = "AHISTORYID"
private long id;

@JoinColumn(name = "STATUSID")
@ManyToOne
private Status status;

public AHistory(Status status){
this.status = status;
}

// other irrelevant fields in BaseBusinessObject class.
}

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name = "STATUSTABLE")
public class Status extends BaseBusinessType {

@Id
@Column(name = "STATUSID")
private long id;

// other irrelevant fields in BaseBusinessType class.
}

@Stateless
@LocalBean
public class SomeEJB{
@PersistenceContext
EntityManager em;

@EJB
ProcessorBean processor;
...
public A logNewA()
throws SystemException {
Status status = em.find(/*...NEW-STATUS*/); //assume this is now attached
A a = new A(status);
return processor.saveA(a);
}

}

@Stateless
@LocalBean
public class ProcessorBean {
@PersistenceContext
EntityManager em;
...
public A saveA(A a) throws SystemException {
AHistory history = new AHistory(a.getStatus());
process.getAHistorys().add(history);
return em.merge(a);
}
}

Caused by: <openjpa-2.1.2-SNAPSHOT-r422266:1497841 nonfatal user error> org.apache.openjpa.persistence.InvalidStateException: Encountered unmanaged object "au.com.combined.domain.Status-1" in life cycle state unmanaged while cascading persistence via field "au.com.combined.domain.AHistory.status" during flush. However, this field does not allow cascade persist. You cannot flush unmanaged objects or graphs that have persistent associations to unmanaged objects.
Suggested actions: a) Set the cascade attribute for this field to CascadeType.PERSIST or CascadeType.ALL (JPA annotations) or "persist" or "all" (JPA orm.xml),
b) enable cascade-persist globally,
c) manually persist the related field value prior to flushing.
d) if the reference belongs to another context, allow reference to it by setting StoreContext.setAllowReferenceToSiblingContext().
FailedObject: au.com.combined.domain.Status-1
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(SingleFieldManager.java:775)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:610)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:578)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:494)
at org.apache.openjpa.kernel.StateManagerImpl.preFlush(StateManagerImpl.java:2971)
at org.apache.openjpa.kernel.PNewProvisionalState.nonprovisional(PNewProvisionalState.java:45)
at org.apache.openjpa.kernel.StateManagerImpl.nonprovisional(StateManagerImpl.java:1222)
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPC(SingleFieldManager.java:812)
at org.apache.openjpa.kernel.SingleFieldManager.preFlushPCs(SingleFieldManager.java:751)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:653)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:578)
at org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:494)
at org.apache.openjpa.kernel.StateManagerImpl.preFlush(StateManagerImpl.java:2971)
at org.apache.openjpa.kernel.PNewState.beforeFlush(PNewState.java:40)
at org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(StateManagerImpl.java:1047)
at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2096)
at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:2056)
at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1974)
at com.ibm.ws.uow.ComponentContextSynchronizationWrapper.beforeCompletion(ComponentContextSynchronizationWrapper.java:65)
at com.ibm.tx.jta.impl.RegisteredSyncs.coreDistributeBefore(RegisteredSyncs.java:291)
at com.ibm.ws.tx.jta.RegisteredSyncs.distributeBefore(RegisteredSyncs.java:152)
at com.ibm.ws.tx.jta.TransactionImpl.prePrepare(TransactionImpl.java:2367)
at com.ibm.ws.tx.jta.TransactionImpl.stage1CommitProcessing(TransactionImpl.java:575)
at com.ibm.tx.jta.impl.TransactionImpl.processCommit(TransactionImpl.java:1015)
... 85 more

最佳答案

已解决

问题在于,在 saveA 函数(为了保密起见,我对其进行了解释)中,我调用了一个 Bean,该 Bean 引用了不同的持久性上下文。在 @PersistenceContext(unitName = "Domain") EntityManager em 中明确说明单位名称后对于 bean,问题就消失了。

因为另一个实体管理器正在查找 Status 对象,所以不同的实体管理器(负责实际调用 em.merge())没有将该对象识别为托管对象,因此它尝试保留该对象,从而导致错误。

关于java - 在与非托管对象具有持久关联的 jpa 实体 : You cannot flush unmanaged objects. 中设置外键时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20062056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com