Java Mailing List Archive

http://www.gg3721.com/

Home » users.openjpa »

avoid calling evict?

andiqo

2008-08-26

Replies: Find Java Web Hosting

Author LoginPost Reply

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
--
Sent from the OpenJPA Users mailing list archive at Nabble.com.

©2008 gg3721.com - Jax Systems, LLC, U.S.A.