You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Tore Halset <ha...@pvv.ntnu.no> on 2006/04/06 21:19:11 UTC

finding the correct DataNode

Hello!

For any given Persistent object, is it possible to find the DataNode  
used? How?

PS: Looking forward to try out B1!

  - Tore.

Re: finding the correct DataNode

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Apr 6, 2006, at 11:19 PM, Tore Halset wrote:

> Hello!
>
> For any given Persistent object, is it possible to find the  
> DataNode used? How?
>
> PS: Looking forward to try out B1!
>
>  - Tore.

Hi Tore,

Here is a 1.1-ish solution that assumes that your ObjectContext is a  
DataContext:

Persistent object = ..
DataContext context = (DataContext) object.getObjectContext();
DataDomain domain = context.getParentDataDomain();
DataMap map = context.getEntityResolver().getObjEntity 
(object.getObjectId().getEntityName()).getDataMap();
DataNode node = domain.lookupDataNode(map);


1.2-ish solution should make no assumptions about the stack  
structure, so it will have to do the lookup indirectly via a custom  
query. Here is such a query. The trick is to override "route" method,  
obtaining QueryEngine from QueryRouter and store it in the query:

public class DataNodeQuery extends AbstractQuery {

     protected DataNode node;
     protected String entityName;

     public DataNodeQuery(Persistent object) {
        this.entityName = object.getObjectId().getEntityName();
     }

     public void route(QueryRouter router, EntityResolver resolver,  
Query substitutedQuery) {
        DataMap map = resolver.getObjEntity(entityName).getDataMap();
        this.node = (DataNode) router.engineForDataMap(map);
     }

     public SQLAction createSQLAction(SQLActionVisitor visitor) {
         throw new UnsupportedOperationException();
     }

     public DataNode getDataNode() {
         return node;
     }
}


Execute the query, but ignore returned result:

DataNodeQuery q = new DataNodeQuery(object);
object.getObjectContext().performGenericQuery(q);
DataNode node = q.getDataNode();

Andrus