You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Dave Lamy <da...@gmail.com> on 2009/04/02 18:53:36 UTC

Quick query question

I am composing a SelectQuery and want to include an expression to narrow my
results based upon the primary key (just an int ID) as well as other
criteria.  I am using the ExpressionFactory to generate the Expressions..
specifically I have

Expression expr = ExpressionFactory.inExp(<pathToId?>, idCollection);

Question is:  How do I represent the path to the primary key in an
expression string like this?

Thanks--
Dave

Re: Quick query question

Posted by Dave Lamy <da...@gmail.com>.
Actually I totally lied just then.  Didn't like the "db:".. had to remove.
My initial test case turned out to be faulty.  Guess I need a test case for
my test case :-).

Anyway thanks again.

On Tue, Apr 7, 2009 at 11:37 AM, Dave Lamy <da...@gmail.com> wrote:

> That did it Mike-- great call!  Thanks for the assist.  Didn't have to
> remove the db: .. not sure if it would work without it or not.
>
> Dave
>
>
> On Mon, Apr 6, 2009 at 8:10 AM, Michael Gentry <mg...@masslight.net>wrote:
>
>> Maybe instead of:
>>
>> Expression expr = ExpressionFactory.inExp("db:" + pkName, idValues);
>>
>> Use:
>>
>> Expression expr = ExpressionFactory.inDbExp("db:" + pkName, idValues);
>>
>>
>> I've never used inDbExp() before, though.  It may automatically assume
>> the "db:" part, so you might have to omit it.
>>
>> mrg
>>
>
>

Re: Quick query question

Posted by Dave Lamy <da...@gmail.com>.
That did it Mike-- great call!  Thanks for the assist.  Didn't have to
remove the db: .. not sure if it would work without it or not.

Dave

On Mon, Apr 6, 2009 at 8:10 AM, Michael Gentry <mg...@masslight.net>wrote:

> Maybe instead of:
>
> Expression expr = ExpressionFactory.inExp("db:" + pkName, idValues);
>
> Use:
>
> Expression expr = ExpressionFactory.inDbExp("db:" + pkName, idValues);
>
>
> I've never used inDbExp() before, though.  It may automatically assume
> the "db:" part, so you might have to omit it.
>
> mrg
>

Re: Quick query question

Posted by Michael Gentry <mg...@masslight.net>.
Maybe instead of:

Expression expr = ExpressionFactory.inExp("db:" + pkName, idValues);

Use:

Expression expr = ExpressionFactory.inDbExp("db:" + pkName, idValues);


I've never used inDbExp() before, though.  It may automatically assume
the "db:" part, so you might have to omit it.

mrg

Re: Quick query question

Posted by Dave Lamy <da...@gmail.com>.
Well unfortunately this doesn't appear to be working.  This is my first time
to try and use the db: prefix in an Expression and from what I can tell no
code actually attempted to parse it.  Am I misusing this prefix capability?

All I'm really doing is

1) determine the PK name (assumption of a single PK, since we are
controlling the schema):

                        ObjEntity entity = resolver.getObjEntity(className);
                        String pkName =
entity.getPrimaryKeyNames().iterator().next();

This appears to work fine.. pkName == "ID"

2) Create an IN expression with my group of id ints
Expression expr = ExpressionFactory.inExp("db:" + pkName, idValues);

3) Potentially join that expression with some others using
Expression.joinExp()

4) create new SelectQuery:
SelectQuery q = new SelectQuery(className, expr);

5) Fire away
retList = performQuery(q);

I get the following exception:

org.apache.cayenne.exp.ExpressionException: [v.3.0M5 Nov 29 2008 21:12:47]
Can't resolve path component: [Advertiser.db:ID].
    at
org.apache.cayenne.map.PathComponentIterator.invalidPathException(PathComponentIterator.java:145)
    at
org.apache.cayenne.map.PathComponentIterator.next(PathComponentIterator.java:133)
    at
org.apache.cayenne.map.PathComponentIterator.next(PathComponentIterator.java:33)
    at
org.apache.cayenne.access.trans.QueryAssemblerHelper.appendObjPath(QueryAssemblerHelper.java:91)
    at
org.apache.cayenne.access.trans.QualifierTranslator.objectNode(QualifierTranslator.java:343)
    at org.apache.cayenne.exp.Expression.traverse(Expression.java:464)
    at org.apache.cayenne.exp.Expression.traverse(Expression.java:461)
    at org.apache.cayenne.exp.Expression.traverse(Expression.java:441)
    at
org.apache.cayenne.access.trans.QualifierTranslator.doAppendPart(QualifierTranslator.java:65)
    at
org.apache.cayenne.access.trans.QueryAssemblerHelper.appendPart(QueryAssemblerHelper.java:73)
    at
org.apache.cayenne.access.trans.SelectTranslator.createSqlString(SelectTranslator.java:117)
....

Is there a problem with me using a db: prefixed path in a normal
SelectQuery?  Can anyone point me at the code that attempts to do this db:
parse so I can try and step through?  I'm rather stumped on this one.

Thanks!
Dave

On Thu, Apr 2, 2009 at 1:36 PM, Dave Lamy <da...@gmail.com> wrote:

> Thanks guys.
>
> I'm actually in the middle of putting together some code that will grab the
> ObjEntity and use the getPrimaryKeyNames() method to get the name of the
> PK.. then just create the expression using "db:" + pkName.  I'll let
> everyone know how that goes.. if not great then I can back up to use this
> instead.
>
> Dave
>
>
> On Thu, Apr 2, 2009 at 1:30 PM, Andrey Razumovsky <
> razumovsky.andrey@gmail.com> wrote:
>
>> This is possible. See
>> http://cwiki.apache.org/CAY/mapping-primary-keys.html
>> Also, as far as I know new modeler has checkbox option for auto-generating
>> primary key attrs at reverse-engineering
>>
>> 2009/4/2 Dave Lamy <da...@gmail.com>
>>
>> > No, I didn't.. just did the standard Cayenne mapping with a database ID
>> > column (auto-generated) which of course is not exposed in the Java
>> class.
>> > What makes this even more fun is that I'm using a generic persistent
>> class
>> > in several cases, so I don't have concrete Java classes/paths to work
>> with.
>> > Was hoping to use some sort of pseudo-path like Hibernate's ".id" field
>> in
>> > hql.
>> >
>> > So far the only real solution to this that I can see is to use a
>> consistent
>> > PK column naming scheme.. like "ID".. and then use a db:ID path
>> expression.
>> > But it's not ideal.
>> >
>> > On Thu, Apr 2, 2009 at 12:01 PM, Michael Gentry <mgentry@masslight.net
>> > >wrote:
>> >
>> > > Did you map your primary key as a Java attribute?  If so, just include
>> > it.
>> > >
>> > >
>> > > On Thu, Apr 2, 2009 at 12:53 PM, Dave Lamy <da...@gmail.com>
>> wrote:
>> > > > I am composing a SelectQuery and want to include an expression to
>> > narrow
>> > > my
>> > > > results based upon the primary key (just an int ID) as well as other
>> > > > criteria.  I am using the ExpressionFactory to generate the
>> > Expressions..
>> > > > specifically I have
>> > > >
>> > > > Expression expr = ExpressionFactory.inExp(<pathToId?>,
>> idCollection);
>> > > >
>> > > > Question is:  How do I represent the path to the primary key in an
>> > > > expression string like this?
>> > > >
>> > > > Thanks--
>> > > > Dave
>> > > >
>> > >
>> >
>>
>
>

Re: Quick query question

Posted by Dave Lamy <da...@gmail.com>.
Thanks guys.

I'm actually in the middle of putting together some code that will grab the
ObjEntity and use the getPrimaryKeyNames() method to get the name of the
PK.. then just create the expression using "db:" + pkName.  I'll let
everyone know how that goes.. if not great then I can back up to use this
instead.

Dave

On Thu, Apr 2, 2009 at 1:30 PM, Andrey Razumovsky <
razumovsky.andrey@gmail.com> wrote:

> This is possible. See
> http://cwiki.apache.org/CAY/mapping-primary-keys.html
> Also, as far as I know new modeler has checkbox option for auto-generating
> primary key attrs at reverse-engineering
>
> 2009/4/2 Dave Lamy <da...@gmail.com>
>
> > No, I didn't.. just did the standard Cayenne mapping with a database ID
> > column (auto-generated) which of course is not exposed in the Java class.
> > What makes this even more fun is that I'm using a generic persistent
> class
> > in several cases, so I don't have concrete Java classes/paths to work
> with.
> > Was hoping to use some sort of pseudo-path like Hibernate's ".id" field
> in
> > hql.
> >
> > So far the only real solution to this that I can see is to use a
> consistent
> > PK column naming scheme.. like "ID".. and then use a db:ID path
> expression.
> > But it's not ideal.
> >
> > On Thu, Apr 2, 2009 at 12:01 PM, Michael Gentry <mgentry@masslight.net
> > >wrote:
> >
> > > Did you map your primary key as a Java attribute?  If so, just include
> > it.
> > >
> > >
> > > On Thu, Apr 2, 2009 at 12:53 PM, Dave Lamy <da...@gmail.com> wrote:
> > > > I am composing a SelectQuery and want to include an expression to
> > narrow
> > > my
> > > > results based upon the primary key (just an int ID) as well as other
> > > > criteria.  I am using the ExpressionFactory to generate the
> > Expressions..
> > > > specifically I have
> > > >
> > > > Expression expr = ExpressionFactory.inExp(<pathToId?>, idCollection);
> > > >
> > > > Question is:  How do I represent the path to the primary key in an
> > > > expression string like this?
> > > >
> > > > Thanks--
> > > > Dave
> > > >
> > >
> >
>

Re: Quick query question

Posted by Andrey Razumovsky <ra...@gmail.com>.
This is possible. See http://cwiki.apache.org/CAY/mapping-primary-keys.html
Also, as far as I know new modeler has checkbox option for auto-generating
primary key attrs at reverse-engineering

2009/4/2 Dave Lamy <da...@gmail.com>

> No, I didn't.. just did the standard Cayenne mapping with a database ID
> column (auto-generated) which of course is not exposed in the Java class.
> What makes this even more fun is that I'm using a generic persistent class
> in several cases, so I don't have concrete Java classes/paths to work with.
> Was hoping to use some sort of pseudo-path like Hibernate's ".id" field in
> hql.
>
> So far the only real solution to this that I can see is to use a consistent
> PK column naming scheme.. like "ID".. and then use a db:ID path expression.
> But it's not ideal.
>
> On Thu, Apr 2, 2009 at 12:01 PM, Michael Gentry <mgentry@masslight.net
> >wrote:
>
> > Did you map your primary key as a Java attribute?  If so, just include
> it.
> >
> >
> > On Thu, Apr 2, 2009 at 12:53 PM, Dave Lamy <da...@gmail.com> wrote:
> > > I am composing a SelectQuery and want to include an expression to
> narrow
> > my
> > > results based upon the primary key (just an int ID) as well as other
> > > criteria.  I am using the ExpressionFactory to generate the
> Expressions..
> > > specifically I have
> > >
> > > Expression expr = ExpressionFactory.inExp(<pathToId?>, idCollection);
> > >
> > > Question is:  How do I represent the path to the primary key in an
> > > expression string like this?
> > >
> > > Thanks--
> > > Dave
> > >
> >
>

Re: Quick query question

Posted by Dave Lamy <da...@gmail.com>.
No, I didn't.. just did the standard Cayenne mapping with a database ID
column (auto-generated) which of course is not exposed in the Java class.
What makes this even more fun is that I'm using a generic persistent class
in several cases, so I don't have concrete Java classes/paths to work with.
Was hoping to use some sort of pseudo-path like Hibernate's ".id" field in
hql.

So far the only real solution to this that I can see is to use a consistent
PK column naming scheme.. like "ID".. and then use a db:ID path expression.
But it's not ideal.

On Thu, Apr 2, 2009 at 12:01 PM, Michael Gentry <mg...@masslight.net>wrote:

> Did you map your primary key as a Java attribute?  If so, just include it.
>
>
> On Thu, Apr 2, 2009 at 12:53 PM, Dave Lamy <da...@gmail.com> wrote:
> > I am composing a SelectQuery and want to include an expression to narrow
> my
> > results based upon the primary key (just an int ID) as well as other
> > criteria.  I am using the ExpressionFactory to generate the Expressions..
> > specifically I have
> >
> > Expression expr = ExpressionFactory.inExp(<pathToId?>, idCollection);
> >
> > Question is:  How do I represent the path to the primary key in an
> > expression string like this?
> >
> > Thanks--
> > Dave
> >
>

Re: Quick query question

Posted by Michael Gentry <mg...@masslight.net>.
Did you map your primary key as a Java attribute?  If so, just include it.


On Thu, Apr 2, 2009 at 12:53 PM, Dave Lamy <da...@gmail.com> wrote:
> I am composing a SelectQuery and want to include an expression to narrow my
> results based upon the primary key (just an int ID) as well as other
> criteria.  I am using the ExpressionFactory to generate the Expressions..
> specifically I have
>
> Expression expr = ExpressionFactory.inExp(<pathToId?>, idCollection);
>
> Question is:  How do I represent the path to the primary key in an
> expression string like this?
>
> Thanks--
> Dave
>