cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrus Adamchik <and...@objectstyle.org>
Subject Re: finding the correct DataNode
Date Fri, 07 Apr 2006 08:06:32 GMT

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



Mime
View raw message