Java openjpa,更新,错误';PK具有非默认值';
我想知道是否有人遇到过这个错误,并能解释发生了什么:Java openjpa,更新,错误';PK具有非默认值';,java,jpa,db2,websphere,openjpa,Java,Jpa,Db2,Websphere,Openjpa,我想知道是否有人遇到过这个错误,并能解释发生了什么: <openjpa-2.1.1-SNAPSHOT-r422266:1087028 nonfatal user error> org.apache.openjpa.persistence.InvalidStateException: Primary key field com.qbe.config.bean.QBEPropertyHistory.id of com.qbe.config.bean.QBEPropertyHistory
<openjpa-2.1.1-SNAPSHOT-r422266:1087028 nonfatal user error>
org.apache.openjpa.persistence.InvalidStateException:
Primary key field com.qbe.config.bean.QBEPropertyHistory.id of com.qbe.config.bean.QBEPropertyHistory@1c710ab has non-default value.
The instance life cycle is in PNewProvisionalState state and hence an
existing non-default value for the identity field is not permitted.
You either need to remove the @GeneratedValue annotation or modify the
code to remove the initializer processing.
在服务层中,我使用find(id)方法加载一个属性,实例化一个新的PropertyHistory,将其添加到property.getHistory()中。添加(propHist),然后调用update(prop)并获取上述错误
如果在find()中关闭EntityManager,错误就会消失,但这会中断延迟加载,并且prop.getHistory()始终返回null。如果我设置fetch=EAGER,它会变得非常慢,因为有10到1000条记录,我需要一次选择数千个属性对象,99.99%的时间不需要历史记录
我无法删除@GeneratedValue,因为它是生成的(DB2,autoincrement)。现在我想知道如何“修改代码以删除初始值设定项处理”
谢谢 问题在于,您正在尝试跨持久性上下文共享实体(
EntityManager
)。您可以更改方法,以获取EntityManager
实例,并使用相同的EM进行查找和更新操作。Hmm。。。很明显,只有当有一个用户时,这个方法才有效,一旦有两个用户,就会抛出这个错误:org.apache.openjpa.persistence.PersistenceException:多个并发线程试图访问单个代理。默认情况下,代理不是线程安全的;如果您需要和/或希望一个代理被多个线程访问,请将openjpa.multi-threaded属性设置为true以覆盖默认行为
@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.MERGE, orphanRemoval=false)
@JoinColumn(name="PROPERTY_NAME")
@OrderBy("updatedTime DESC")
private List<QBEPropertyHistory> history = new ArrayList<QBEPropertyHistory>();
public T find(Object id) {
T t = null;
synchronized(this) {
EntityManager em = getEm();
t = em.find(type, id);
//em.close(); //If this is uncommented, fetch=LAZY doesn't work. And fetch=EAGER is too slow.
}
return t;
}
public T update(T t) {
synchronized(this) {
EntityManager em = getEm();
em.getTransaction().begin();
t = em.merge(t);
em.getTransaction().commit();
em.close();
return t;
}
}