Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] EntityManager.remove does not remove entity from cache

Hi Mike,

You need to sever the relationship between an A and a B before you remove B. JPA does not provide any magic for this.

-Tom

Mike Traum wrote:
I have the following enitiy map:
A->B->C (where -> represents a OneToMany relationship)

If I do a EntityManager.remove on a B, a following Query (select all) will still return that entity, even though it has been removed from the database.

Any ideas on the proper way to handle this?

Here's some code illustrating the issue:
   EntityManager em = emf.createEntityManager();
   Query q = em.createQuery("select a FROM A a");
   List<A> as = q.getResultList();
   em.clear();
   em.close();
   System.out.println(as.get(0).getChildren().size()); // output is 2
     B b = as.get(0).getChildren().get(0);
     em = emf.createEntityManager();
   em.getTransaction().begin();
   B bm = em.merge(b);
   em.remove(bm);
em.getTransaction().commit(); // successfully removes B and children from database
   em.clear();
   em.close();

   em = emf.createEntityManager();
   q = em.createQuery("select a FROM A a");
   as = q.getResultList();
   em.clear();
   em.close();
   System.out.println(as.get(0).getChildren().size()); // output is 2
     em = emf.createEntityManager();
   q = em.createQuery("select a FROM A a");
((ReadAllQuery)((EJBQueryImpl)q).getDatabaseQuery()).refreshIdentityMapResult();
   as = q.getResultList();
   em.clear();
   em.close();
   System.out.println(as.get(0).getChildren().size()); // output is 1


Thanks,
Mike

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


Back to the top