也许这个问题已经回答了,也许没有。
我想用乐观锁更新一个对象,使用
...
EntityManager.merge(...);
EntityManager.flush(...);
...
我的代码工作正常。我想要的是更新对象中的版本号并保留对象引用。
我谷歌搜索了很长时间,但我没有找到答案。
我最后的手段是制作一个 nativeQuery 并获取版本号。
这是一个测试代码:
EntityManagerFactory factory = Persistence.createEntityManagerFactory(DBManager.getUnidadPersistencia());
EntityManager emA = factory.createEntityManager();
Grupo g = emA.find(Grupo.class, 1L);
emA.close();
//In some place
g.getSolicitanteList().get(0).setObservaciones("ZZZZ"); //Originally XXXX
//...
//Then I have to save the changes
EntityManager emB = factory.createEntityManager();
EntityTransaction tx = emB.getTransaction();
try {
tx.begin();
emB.merge(g);
emB.flush();
tx.commit();
//Here comes exception
emB.refresh(g);
} catch (Exception e) {
if (tx.isActive()) {
tx.rollback();
}
logger.log(Level.SEVERE, null, e);
}
emB.close();
和堆栈跟踪:
30/07/2012 05:15:31 PM org.rp.co5parser.TestGrupo refreshTest
GRAVE: null
java.lang.IllegalArgumentException: Can not refresh not managed object: org.rp.co5parser.alojamiento.Grupo[ idGrupo=1 ].
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.refresh(EntityManagerImpl.java:943)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.refresh(EntityManagerImpl.java:849)
at org.rp.co5parser.TestGrupo.refreshTest(TestGrupo.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.tools.ant.taskdefs.optional.junit.JUnit4TestMethodAdapter.run(JUnit4TestMethodAdapter.java:109)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:520)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1060)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:911)
那样行吗?
提前致谢....