Re: Path Expression => Attribute

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Fri Apr 07 2006 - 08:55:31 EDT

  • Next message: Bryan Lewis: "upgrading to 1.2B1"

    You need to prefix DB_PATH expressions with "db:". This is how
    expression parser distinguishes between the two types of paths. E.g.:

       public DbAttribute pathToDbAttribute(Class baseClass, String path) {
          if(!path.startsWith("db:")) {
             path = "db:" + path;
          }

          ...
       }

    Andrus

    On Apr 7, 2006, at 4:40 PM, Tobias SCHOESSLER wrote:

    > Andrus, this worked fine, thank you. I use this method to lookup the
    > ObjAttribute for a given path now.
    >
    > public ObjAttribute pathToObjAttribute(Class baseClass,
    > String path)
    > {
    > ObjEntity rootEntity =
    > ctxt.getEntityResolver().lookupObjEntity(baseClass);
    > ObjAttribute attribute =
    > (ObjAttribute)Expression.fromString(path).evaluate(rootEntity);
    > return attribute;
    > }
    >
    > In the simplest case I feed this the name of an attribute in the
    > baseClass
    > and it returns me the ObjAttribute. e.G. 'countryCode'
    >
    > However it turned out that I rather need to use Db Paths to also be
    > able to
    > specify PK values. I changed the method to
    >
    > public DbAttribute pathToDbAttribute(Class baseClass, String
    > path) {
    > DbEntity rootEntity =
    > ctxt.getEntityResolver().lookupDbEntity(baseClass);
    > DbAttribute attribute =
    > (DbAttribute)Expression.fromString(path).evaluate(rootEntity);
    > return attribute;
    > }
    >
    > I expect to be able to feed it the corresponding database attribute
    > name
    > now e.G. COUNTRY_CODE but this fails with the exception
    >
    > Caused by: org.objectstyle.cayenne.exp.ExpressionException: [v.1.1.3
    > September 28 2005] Invalid expression type: 'ObjPath', DB_PATH is
    > expected.
    > at
    > org.objectstyle.cayenne.map.DbEntity.resolvePathComponents
    > (DbEntity.java:249)
    > at
    > org.objectstyle.cayenne.exp.parser.ASTPath.evaluateEntityNode
    > (ASTPath.java:108)
    > at
    > org.objectstyle.cayenne.exp.parser.ASTObjPath.evaluateNode
    > (ASTObjPath.java:85)
    > at
    > org.objectstyle.cayenne.exp.parser.SimpleNode.evaluate
    > (SimpleNode.java:243)
    > ... 43 more
    >
    >
    >
    >
    >
    >
    > Andrus Adamchik
    > <andru..bjectsty
    >
    > le.org> To
    > cayenne-
    > use..ncubator.apache.org
    > Monday, 3
    > April cc
    > 2006 16:44
    >
    > Subject
    > Re: Path Expression =>
    > Attribute
    > Please respond to
    > cayenne-use..ncu
    > bator.apache.org
    >
    >
    >
    >
    >
    >
    >
    > You can evaluate an expression with root ObjEntity:
    >
    > String path = "toArtist.exhibitArray.closingDate";
    > ObjEntity rootEntity = context.getEntityResolver().lookupObjEntity
    > (Painting.class);
    > ObjAttribute attribute = Expression.fromString(path).evaluate
    > (rootEntity);
    >
    > Andrus
    >
    > On Apr 3, 2006, at 6:36 PM, Tobias SCHOESSLER wrote:
    >
    >> hi,
    >>
    >> How do I get from a path expression to the resulting attribute
    >> object.
    >>
    >> E.g. Is there anything that I can use to parse a path like
    >>
    >> toArtist.exhibitArray.closingDate
    >>
    >> and get the corresponding ObjAttribute for closingDate.
    >>
    >> As a precondition i would assume that the path evaluates to an
    >> attribute.
    >>
    >> thanks
    >>
    >> regards
    >>
    >> Tobias
    >>
    >>
    >
    >
    >
    >



    This archive was generated by hypermail 2.0.0 : Fri Apr 07 2006 - 08:56:06 EDT