我正在尝试运行这个基本的JPA/EJB代码:
public static void main(String[] args){
UserBean user = new UserBean();
user.setId(1);
user.setUserName("name1");
user.setPassword("passwd1");
em.persist(user);
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.JPA.Database
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我在互联网上搜索,我找到的原因是:
这是由您创建对象的方式引起的,即如果您明确设置了ID属性.删除ID分配修复了它.
但我没有得到它,我需要修改什么来使代码工作?
我收到异常org.hibernate.PersistentObjectException:传递给persist的分离实体.从本论坛和其他地方的众多帖子中,我了解到这种情况发生在两种情况下(不考虑One-One注释等),
我看到这些都没有发生在我的代码中.我无法重现错误,因为我没有最初触发它的数据.在其他数据上,它运行得很好.我在下面提供了一个SCCE:
public class MyProcessor {
private MyImportEJB myEJB = MyImportEJB.getInstance();
private List<MyClass> saveQueue = new ArrayList<MyClass>();
public void process() {
List<X> rawData = getListOfX();
for(X x:rawData) {
processX();
}
saveFoos(saveQueue);
}
public void saveOrUpdateFoos(List<Foo> foos) {
for(MyClass foo:foos) {
MyClass existingFoo = myEJB.getFoosForWidAndDates(foo.getWid(), foo.getEffBeginDt(),foo.getEffEndDt());
if(existingFoo == null) saveQueue.add(foo);
else {
existingFoo.updateIfDifferent(foo);
saveQueue.add(existingFoo);
}
}
if(saveQueue.size() > 5000) {
myEJB.saveObjects(saveQueue);
saveQueue.clear();
}
}
public void processX() {
ArrayList<MyClass> foos = new ArrayList<MyClass>();
if(x.reportPeriod != null && x.gravity != …Run Code Online (Sandbox Code Playgroud)