eclipselink,高级查询无法按预期工作

时间:2013-07-18 19:29:23

标签: jpa persistence eclipselink

我正在使用eclipselink for jpa,迭代器工作奇怪...... 在使用高级查询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

1 个答案:

答案 0 :(得分:1)

您要解包的会话可以访问二级缓存;它将返回只读且不应更改的对象。不跟踪对这些对象的任何更改,并且由于它们是来自共享高速缓存的对象,因此对其他线程立即可见。

您应该获得的是UnitOfWork,它将返回托管实体。