Eclipselink history of related objects

核能气质少年 提交于 2019-12-03 15:38:47

In order to query the historical state of several - not just one like above - entities, we have to create an EclipseLink specific HistoricalSession. Queries run through this session will use the same historical timestamp and represent the proper historical state of the object graph.

I am using JPA in other parts of the code, so I will start with converting the JPA Query to an EclipseLink ReadAllQuery.

The HistoricalSession has its own entity cache, so that the historical entities do not mix with the normal ones.

        // Get the EclipseLink ServerSession from the JPA EntitiyManagerFactory
        Server serverSession = JpaHelper.getServerSession(emf);
        // Only a ClientSession can give us a HistoricalSession so ask one from the ServerSession 
        ClientSession session = serverSession.acquireClientSession();
        // Create the HistoricalSessions. A HistoricalSession is sticked to a point in the past and all the queries are executed at that time.
        Session historicalSessionAfterFirstChild = session.acquireHistoricalSession(new AsOfClause(afterFirstChildAdded));

        ReadAllQuery q; 

        Query jpaQuery = em.createQuery(query);
        jpaQuery.setParameter("root", "parent");
        // Extract the EclipseLink ReadAllQuery from the JPA Query. We can use named queries this way.
        q=JpaHelper.getReadAllQuery(jpaQuery);

        // This is a possible EclipseLink bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=441193
        List<Object> arguments = new Vector<Object>();
        arguments.add("parent");
        q.setArgumentValues(arguments);

        Vector<Parent> historyAwareParents ;

        // Execute the query
        historyAwareParents = (Vector<Parent>) historicalSessionAfterFirstChild.executeQuery(q);
        for (Child c : historyAwareParents.get(0).children) {
            System.out.println(c.getExtension() + " " + c.getRoot());
        }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!