Hi,

A question for JPA experts in order to avoid calling evict, please.

I have a simple model made of 2 classes : Node and Link to modelize a graph
in database (Link = many-to-many relationship). The Link class has a 'kind'
attribute, in order to know if the association between Nodes is an
aggregation or a composition. Thus, as I cannot use 'dependent-element' tag,
I manage deletion by code.

So, when the user chooses to delete one node somewhere in the graph, a
delete action manages it and implements a 'cascade-delete' in database for
hard links. But I need to call evict potentially a lot of times, in order to
avoid stale nodes...

Doing something like:

// --
  // Avoid stale children in other Node parents
  queryString = "SELECT FROM_ID FROM LINK AS l WHERE l.TO_ID=" + id;      
  query = getEntityManager().createNativeQuery(queryString);
  List<?> list = query.getResultList();
  for (Object parent : list) {
    queryString = "SELECT n FROM Node n WHERE n.id = " + (Long) parent;
    query = getEntityManager().createQuery(queryString);
    Node node = (Node) query.getSingleResult();
    OpenJPAEntityManager openJPAEM =
OpenJPAPersistence.cast(getEntityManager());
    openJPAEM.evict(node);
  }
// --

Thus I create a native query using only ids, then a JPA query to get the
Node object, then call evict. 
But most of the time, the parent Node I tried to evict isn't even loaded in
the EntityManager. Thus my question is, do you see better way to do this?

Thanks a lot for your help.

Nicolas
-- 
View this message in context: 
http://n2.nabble.com/avoid-calling-evict--tp783696p783696.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to