You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Tobias SCHOESSLER <To...@unvienna.org> on 2006/04/03 16:36:01 UTC
Path Expression => Attribute
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
Re: Path Expression => Attribute
Posted by Andrus Adamchik <an...@objectstyle.org>.
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
> <andrus@objectsty
>
> le.org> To
> cayenne-
> user@incubator.apache.org
> Monday, 3
> April cc
> 2006 16:44
>
> Subject
> Re: Path Expression =>
> Attribute
> Please respond to
> cayenne-user@incu
> 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
>>
>>
>
>
>
>
Re: Path Expression => Attribute
Posted by Tobias SCHOESSLER <To...@unvienna.org>.
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
<andrus@objectsty
le.org> To
cayenne-user@incubator.apache.org
Monday, 3 April cc
2006 16:44
Subject
Re: Path Expression => Attribute
Please respond to
cayenne-user@incu
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
>
>
Re: Path Expression => Attribute
Posted by Andrus Adamchik <an...@objectstyle.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
>
>