Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] EntityManager life-cycle..

Can you please send the implementation of getEntityManager() and the code that creates the EntityManagerFactory.

Paul Harrison wrote:
On 2011-03 -29, at 08:43, Paul Harrison wrote:

as Racks are the basic containers for all other objects and there need to be two pre-existing for the application to be able to function - stationRack and baselineRack are static members of Rack. The code that does the equivalent of what causes the error below runs fine in a unit test - so it seems that it is running within Netbeans RCP that the problem occurs - perhaps it is to do with an interaction between the classloading requirements of Eclipselink and Netbeans RCP....

Actually I was looking and the unit test does other things first - if I isolate the unit test to do exactly what is happening in Netbeans RCP then exactly the same happens (the NPE) in a pure unit test  - the following is the first (non-static) piece of code

RackJpaController jpa = new RackJpaController();
List<Rack> rackList = jpa.findRackEntities();


so there seems to be an interaction between the static initialization in Rack

    static {
try {
            RackJpaController instance = new RackJpaController();
            if((stationRack = instance.findRack(Integer.valueOf(255))) == null){
                stationRack = new Rack(Integer.valueOf(255));
                stationRack.setName("Station");
                instance.create(stationRack);
            }
            if((baselineRack = instance.findRack(Integer.valueOf(101))) == null){
                baselineRack = new Rack(Integer.valueOf(101));
                baselineRack.setName("Baseline");
                instance.create(baselineRack);
            }
        } catch (PreexistingEntityException e) {
            throw new RuntimeException("A Rack has already been created", e);
        } catch (Exception e) {
            throw new RuntimeException("Error creating rack", e);
        }
}

and the use of Rack as the List generic type specifier - the code for the jpa.findRackEntities() is

   public List<Rack> findRackEntities() {
        return findRackEntities(true, -1, -1);
    }

    public List<Rack> findRackEntities(int maxResults, int firstResult) {
        return findRackEntities(false, maxResults, firstResult);
    }

    private List<Rack> findRackEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Rack.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

I can work round this problem by creating an instance of Rack before the "List<Rack> rackList = jpa.findRackEntities();" to get the static initializers done separately.

I can send you a complete maven project with just the relevant classes if you want to investigate the issue further...

Regards,
	Paul.

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top