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

EclipseLink relies on your DB foreign-key-contstraints to enforce this.

i.e. You should get a SQL exception on the remove if a foreign-key-constraint exists for this relationship.

Mike Traum wrote:
Yes, I thought that might be the answer. But, the implementation seems odd. JPA doesn't provide magic for this, but it is deleted from the database, however the cache is maintained in a stale state. It seems to me that either it should not be deleted from the database or it should be removed from the cache.

mike

Tom Ware wrote:
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
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top