4

I use eclipselink and have the following code:

public Temp getTemp() {
    EntityManager em=emf.createEntityManager();
    String queryString="SELECT a FROM Temp a";
    EntityGraph<Temp> eg = em.createEntityGraph(Temp.class);
    eg.addAttributeNodes("id");
    //eg.addAttributeNodes("name");
    Query query = em.createQuery(queryString);
    query.setHint("javax.persistence.fetchgraph", eg);
    List<Temp> items=query.getResultList();
    em.close();// ENTITYMANAGER IS CLOSED 
    return items.get(0);
}

public void temp(){
    Temp temp=getTemp();
    System.out.println("id:"+temp.getId());
    System.out.println("name:"+temp.getName());
}

As you can see from code we load only id. However, when we do temp.getName() onemore SQL query is executed and necessary data is loaded. Why? We did close entity manager. I expected to get exception at temp.getName().

EDIT 1 After some research I found out the following - the behaviour which I described above is actual when weaving is static (<property name="eclipselink.weaving" value="static"/> + de.empulse.eclipselink weaving plugin). However, when weaving is dynamic (<property name="eclipselink.weaving" value="true"/>) I get exception:

java.lang.ClassNotFoundException: org.eclipse.persistence.internal.jpa.EntityManagerImpl not found by com.temp [57]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1574)
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:79)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2018)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.temp.Temp._persistence_checkFetched(Temp2.java)
    at com.temp.Temp._persistence_get_name(Temp2.java)
    at com.temp.Temp.getName(Temp.java:44)

So question moved here: Eclipselink: Difference between static and dynamic weaving

Community
  • 1
  • 1
Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • 1
    Possible duplicate of [JPA EntityManager.detach() still load lazy relations](http://stackoverflow.com/questions/37562870/jpa-entitymanager-detach-still-load-lazy-relations) – Alan Hay Jun 10 '16 at 07:06

1 Answers1

2

Because that is EclipseLink and it supports detached loading of fields ... i.e something that is not in the JPA spec and is totally non-portable (i.e not supported in other implementations). It's "detached" state is not really "detached", and retains contact with the EntityManagerFactory. If you close the EMF then they become truly "detached".

See this email chain discussing the "feature". It doesn't mention a way of turning it off (so you can have portable behaviour), and in fact asks the person to request it! The question as to why the JPA TCK doesn't check for portable behaviour is one to ask of Oracle ... but hang on, the EclipseLink team wrote the TCK didn't they ... hmmmm..

To stay portable you will detach as per the JPA spec and not rely on such behaviour

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29