我正在为 jpa 使用 eclipselink,并且迭代器工作得很奇怪......在通过不使用高级查询 api 修复它之后,我尝试缩小问题范围。看起来我可能完全误解了手册中的某些内容...为什么以下两个查询有不同的结果?
有问题的代码是这样的:
if (useAdvancedQuery) {
// doesn't write to db
ReadAllQuery readAllQuery = new ReadAllQuery(type);
readAllQuery.useCursoredStream(500, 500);
Session session = em.unwrap(Session.class);
scrollableCursor = (CursoredStream) session
.executeQuery(readAllQuery);
} else {
Query query = em.createQuery("SELECT e FROM " + type.getName()
+ " e ORDER BY e.id Desc");
query.setHint("eclipselink.cursor", true);
query.setHint("eclipselink.cursor.page-size", 50);
scrollableCursor = (CursoredStream) query.getSingleResult();
}
如果 useAdvancedQuery==false那么一切都按预期工作
如果我启用useAdvancedQuery那么它会失败......但是以一种非常奇怪的方式:
- 事务可以正确地使用迭代器写入值
- 来自同一个 entityManagerFactory的不同事务可以看到这些变化
- 但是......在关闭这个EntityManagerFactory并打开一个新的之后,一切都消失了......
我为这个问题写了一个单文件示例:
package x;
import java.io.Serializable;
import java.util.*;
import javax.persistence.*;
import junit.framework.Assert;
import org.eclipse.persistence.queries.*
import org.eclipse.persistence.sessions.Session;
@Entity
public class TestEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
private String label;
private static final long serialVersionUID = 1L;
TestEntity() {
}
public TestEntity(String l) {
setLabel(l);
}
public static void main(String[] args) throws ClassNotFoundException {
new Extracted().run();
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
static class Extracted {
// tweakables
boolean useAdvancedQuery = true;
boolean useNewEntityManagers = false;
public <T> Iterator<T> getIterator(Class<T> type) {
final CursoredStream scrollableCursor;
// change this to false..and it will pass
if (useAdvancedQuery) {
// doesn't write to db
ReadAllQuery readAllQuery = new ReadAllQuery(type);
readAllQuery.useCursoredStream(500, 500);
Session session = em.unwrap(Session.class);
scrollableCursor = (CursoredStream) session
.executeQuery(readAllQuery);
} else {
Query query = em.createQuery("SELECT e FROM " + type.getName()
+ " e ORDER BY e.id Desc");
query.setHint("eclipselink.cursor", true);
query.setHint("eclipselink.cursor.page-size", 50);
scrollableCursor = (CursoredStream) query.getSingleResult();
}
return new Iterator<T>() {
@Override
public boolean hasNext() {
return scrollableCursor.hasMoreElements();
}
@Override
public T next() {
if (!scrollableCursor.hasMoreElements()) {
return null;
}
scrollableCursor.clear();
@SuppressWarnings("unchecked")
T c = (T) scrollableCursor.next();
return c;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
private EntityManagerFactory emf;
private EntityManager em;
public void run() {
{
emf = Persistence.createEntityManagerFactory("ptx2");
em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(new TestEntity("a"));
em.persist(new TestEntity("c"));
em.persist(new TestEntity("d"));
em.getTransaction().commit();
System.out.println("asd!");
em.close();
if (useNewEntityManagers)
emf.close();
}
String testStr = "tst" + new Random().nextInt();
{
if (useNewEntityManagers)
emf = getEntityManager();
em = emf.createEntityManager();
em.getTransaction().begin();
Iterator<TestEntity> it = getIterator(TestEntity.class);
while (it.hasNext()) {
TestEntity e = it.next();
e.setLabel(testStr);
// em.merge(e);
}
em.getTransaction().commit();
em.close();
if (useNewEntityManagers)
emf.close();
}
{
if (useNewEntityManagers)
emf = getEntityManager();
checkConsistance(testStr);
System.out.println("persisted in current session - ok");
emf.close();
}
{
System.out.println("persisted after reconnect?");
emf = getEntityManager();
checkConsistance(testStr);
emf.close();
}
System.out.println("passed");
}
private void checkConsistance(String testStr) {
em = emf.createEntityManager();
em.getTransaction().begin();
Iterator<TestEntity> it = getIterator(TestEntity.class);
while (it.hasNext()) {
TestEntity e = it.next();
Assert.assertEquals(testStr, e.getLabel());
}
em.close();
}
}
private static EntityManagerFactory getEntityManager() {
return Persistence.createEntityManagerFactory("ptx2");
}
}
注意:我正在使用Eclipselink-2.5.0.v20130507-3faac2b