You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Eric Polino <al...@gmail.com> on 2008/02/13 19:48:05 UTC

questions

I've started using Cayenne a few weeks ago for our project,
www.campgroundautomation.com.  In the process of working with it, I've
come across a few things I don't understand.  After reading the API
and the UserGuide, can't find much to help me.

1.  How do I count the rows of a query, without actually doing a full
blown SelectQuery and counting the elements?  ie: select count(*) from
table;
2.  I've read in many places how Cayenne deals with joins for you, but
nowhere do I see an example of how its done.  Here's an example
problem I'm dealing with.

Schema:

Reservation (n to 1) Site
Site (n to 1)
Site contains a type field

I want to get all the Reservations of a given for a given type of
site.  In sql I would execute something like, "select * from
Reservation, Site, SiteType where SiteType.Name='Something' and
Site.typeID = SiteType.id and Reservation.siteID = Site.id"

Short of writing an parameterized sql string and executing that in my
code, how do I do that with Cayenne?  I hope I explained all that well
enough...make sense?

TIA,
Eric


-- 
"None are more hopelessly enslaved than those who falsely believe they
are free."
                                      --Goethe

"Freedom is living without government coercion."
                   --Ron Paul (www.ronpaul2008.com)

Re: questions

Posted by Michael Gentry <bl...@gmail.com>.
Perhaps look at CountQuery here:

http://cayenne.apache.org/doc/customizing-queries.html

/dev/mrg


On Feb 13, 2008 3:08 PM, Eric Polino <al...@gmail.com> wrote:
>
> On Feb 13, 2008 2:36 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
> > On Feb 13, 2008, at 1:48 PM, Eric Polino wrote:
> > > 1.  How do I count the rows of a query, without actually doing a full
> > > blown SelectQuery and counting the elements?  ie: select count(*) from
> > > table;
> >
> > For aggregate queries I suggest EJBQLQuery (only available in 3.0)...
> > It uses the same approach of joins expressed as relationships as
> > SelectQuery, only it also allows aggregate expressions. E.g.:
> >
> >     EJBQLQuery query = new EJBQLQuery("select count(a) from Artist a
> > WHERE a.artistName like 'X%'");
> >     Number count = (Number) DataObjectUtils.objectForQuery(query);
>
> Cool, but we've been having trouble upgrading to 3.0 (we're on 2.0).
> Short of upgrading for this feature, any suggestions on what we can do
> in 2.0?
>
> > Andrus
>
> >
> >
>
>
>
> --
> "None are more hopelessly enslaved than those who falsely believe they
> are free."
>                                       --Goethe
>
> "Freedom is living without government coercion."
>                    --Ron Paul (www.ronpaul2008.com)
>

Re: questions

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Feb 13, 2008, at 3:08 PM, Eric Polino wrote:

> Cool, but we've been having trouble upgrading to 3.0 (we're on 2.0).
> Short of upgrading for this feature, any suggestions on what we can do
> in 2.0?

SQLTemplate.

Andrus


Re: questions

Posted by Eric Polino <al...@gmail.com>.
On Feb 13, 2008 2:36 PM, Andrus Adamchik <an...@objectstyle.org> wrote:
> On Feb 13, 2008, at 1:48 PM, Eric Polino wrote:
> > 1.  How do I count the rows of a query, without actually doing a full
> > blown SelectQuery and counting the elements?  ie: select count(*) from
> > table;
>
> For aggregate queries I suggest EJBQLQuery (only available in 3.0)...
> It uses the same approach of joins expressed as relationships as
> SelectQuery, only it also allows aggregate expressions. E.g.:
>
>     EJBQLQuery query = new EJBQLQuery("select count(a) from Artist a
> WHERE a.artistName like 'X%'");
>     Number count = (Number) DataObjectUtils.objectForQuery(query);

Cool, but we've been having trouble upgrading to 3.0 (we're on 2.0).
Short of upgrading for this feature, any suggestions on what we can do
in 2.0?

> Andrus
>
>



-- 
"None are more hopelessly enslaved than those who falsely believe they
are free."
                                      --Goethe

"Freedom is living without government coercion."
                   --Ron Paul (www.ronpaul2008.com)

Re: questions

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Feb 13, 2008, at 1:48 PM, Eric Polino wrote:
> 1.  How do I count the rows of a query, without actually doing a full
> blown SelectQuery and counting the elements?  ie: select count(*) from
> table;

For aggregate queries I suggest EJBQLQuery (only available in 3.0)...  
It uses the same approach of joins expressed as relationships as  
SelectQuery, only it also allows aggregate expressions. E.g.:

    EJBQLQuery query = new EJBQLQuery("select count(a) from Artist a  
WHERE a.artistName like 'X%'");
    Number count = (Number) DataObjectUtils.objectForQuery(query);

Andrus


Re: questions

Posted by Eric Polino <er...@campgroundautomation.com>.
> No ... we are lucky to have limited web access.  :-)  (Even internal
> web access is limited.)

Wow, either a cool place to work with high security...or just a prison ;)

(this is aluink, i transfered my subscription to my work email)

>
> On Feb 13, 2008 3:40 PM, Eric Polino <al...@gmail.com> wrote:
> > Got ssh? I'd be glad to offer a few tunnels!!
> >
> >
> > On Feb 13, 2008 3:35 PM, Michael Gentry <bl...@gmail.com> wrote:
> > > Some of us are behind rather restrictive firewalls, too.  :-)
> > >
> > >
> > > On Feb 13, 2008 3:31 PM, Kevin Menard <km...@servprise.com> wrote:
> > >
> > > > I've been trying to get the channel going, but most people prefer the email
> > > > list.  You can catch me on IRC during work hours most days (EST).  My nick
> > > > is "nirvdrum".
> > > >
> > > > --
> > > > Kevin
> > >
> >
> >
> >
> >
> > --
> > "None are more hopelessly enslaved than those who falsely believe they
> > are free."
> >                                       --Goethe
> >
> > "Freedom is living without government coercion."
> >                    --Ron Paul (www.ronpaul2008.com)
> >
>
>
>
> --
> "None are more hopelessly enslaved than those who falsely believe they
> are free."
>                                       --Goethe
>
> "Freedom is living without government coercion."
>                    --Ron Paul (www.ronpaul2008.com)
>



-- 
Eric Polino
Campground Automated Systems

Re: questions

Posted by Michael Gentry <bl...@gmail.com>.
No ... we are lucky to have limited web access.  :-)  (Even internal
web access is limited.)


On Feb 13, 2008 3:40 PM, Eric Polino <al...@gmail.com> wrote:
> Got ssh? I'd be glad to offer a few tunnels!!
>
>
> On Feb 13, 2008 3:35 PM, Michael Gentry <bl...@gmail.com> wrote:
> > Some of us are behind rather restrictive firewalls, too.  :-)
> >
> >
> > On Feb 13, 2008 3:31 PM, Kevin Menard <km...@servprise.com> wrote:
> >
> > > I've been trying to get the channel going, but most people prefer the email
> > > list.  You can catch me on IRC during work hours most days (EST).  My nick
> > > is "nirvdrum".
> > >
> > > --
> > > Kevin
> >
>
>
>
>
> --
> "None are more hopelessly enslaved than those who falsely believe they
> are free."
>                                       --Goethe
>
> "Freedom is living without government coercion."
>                    --Ron Paul (www.ronpaul2008.com)
>

Re: questions

Posted by Eric Polino <al...@gmail.com>.
Got ssh? I'd be glad to offer a few tunnels!!

On Feb 13, 2008 3:35 PM, Michael Gentry <bl...@gmail.com> wrote:
> Some of us are behind rather restrictive firewalls, too.  :-)
>
>
> On Feb 13, 2008 3:31 PM, Kevin Menard <km...@servprise.com> wrote:
>
> > I've been trying to get the channel going, but most people prefer the email
> > list.  You can catch me on IRC during work hours most days (EST).  My nick
> > is "nirvdrum".
> >
> > --
> > Kevin
>



-- 
"None are more hopelessly enslaved than those who falsely believe they
are free."
                                      --Goethe

"Freedom is living without government coercion."
                   --Ron Paul (www.ronpaul2008.com)

Re: questions

Posted by Michael Gentry <bl...@gmail.com>.
Some of us are behind rather restrictive firewalls, too.  :-)

On Feb 13, 2008 3:31 PM, Kevin Menard <km...@servprise.com> wrote:

> I've been trying to get the channel going, but most people prefer the email
> list.  You can catch me on IRC during work hours most days (EST).  My nick
> is "nirvdrum".
>
> --
> Kevin

Re: questions

Posted by Eric Polino <al...@gmail.com>.
OK.  I just know as a user it would be much simpler to hop into an IRC
channel to ask some questions instead of joining a mailing list just
to ask a few questions.  I don't mind joining the list myself, since
we'll be using cayenne for most of our project.  So it'll be useful to
keep tabs on what is happening.  But for the average user who doesn't
need more than just a few answers the mailing list might be a
deterrent.  I know this rather well as last summer I was part of the
Pidgin team with Google Summer of Code, and so I hung out in their
channel most of my days, and they were always being hit by pass-by
users with a question.  More elaborate discussions were held on our
mailing list.

Just a thought,
Eric

On Feb 13, 2008 3:27 PM, Michael Gentry <bl...@gmail.com> wrote:
> We mainly use the mailing lists to communicate.  An exception might be
> when putting together a release.
>
> /dev/mrg
>
>
>
> On Feb 13, 2008 3:18 PM, Eric Polino <al...@gmail.com> wrote:
> >
> > Oh, no insult incured.  I'm appreciating the help you guys are giving
> > me, I'm in no position to complain.  I've tried hopping into #cayenne
> > on freenode and there's nobody there?  Is there another channel? or do
> > you all use another means of "chat" communique?
>



-- 
"None are more hopelessly enslaved than those who falsely believe they
are free."
                                      --Goethe

"Freedom is living without government coercion."
                   --Ron Paul (www.ronpaul2008.com)

Re: questions

Posted by Michael Gentry <bl...@gmail.com>.
We mainly use the mailing lists to communicate.  An exception might be
when putting together a release.

/dev/mrg


On Feb 13, 2008 3:18 PM, Eric Polino <al...@gmail.com> wrote:
>
> Oh, no insult incured.  I'm appreciating the help you guys are giving
> me, I'm in no position to complain.  I've tried hopping into #cayenne
> on freenode and there's nobody there?  Is there another channel? or do
> you all use another means of "chat" communique?

Re: questions

Posted by Kevin Menard <km...@servprise.com>.
Certainly.  I wasn't saying the list was broken or anything.  IRC is nice
for the impromptu discussions, but the list is nicer for most other things.
There's room for both synchronous and asynchronous.

-- 
Kevin


On 2/13/08 3:54 PM, "Andrus Adamchik" <an...@objectstyle.org> wrote:

> 
> On Feb 13, 2008, at 3:31 PM, Kevin Menard wrote:
> 
>> I've been trying to get the channel going, but most people prefer
>> the email
>> list.
> 
> There is a reason for that. All of us are volunteers, none of us work
> for the same company and many are in different time zones. The only
> way to get an inclusive discussion and for the free support to work
> *consistently* is to do it *asynchronously* and maintain the archive.
> Hence the mailing list.
> 
> IRC is fine as long as the users understand that there is still a much
> better chance for them to get consistent help on the mailing list. I
> am not surprised that many would prefer to avoid subscribing, but I
> don't see how that could scale for the reasons above.
> 
> Andrus
> 


Re: questions

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Feb 13, 2008, at 3:31 PM, Kevin Menard wrote:

> I've been trying to get the channel going, but most people prefer  
> the email
> list.

There is a reason for that. All of us are volunteers, none of us work  
for the same company and many are in different time zones. The only  
way to get an inclusive discussion and for the free support to work  
*consistently* is to do it *asynchronously* and maintain the archive.  
Hence the mailing list.

IRC is fine as long as the users understand that there is still a much  
better chance for them to get consistent help on the mailing list. I  
am not surprised that many would prefer to avoid subscribing, but I  
don't see how that could scale for the reasons above.

Andrus


Re: questions

Posted by Kevin Menard <km...@servprise.com>.
On 2/13/08 3:18 PM, "Eric Polino" <al...@gmail.com> wrote:

> 
> Oh, no insult incured.  I'm appreciating the help you guys are giving
> me, I'm in no position to complain.  I've tried hopping into #cayenne
> on freenode and there's nobody there?  Is there another channel? or do
> you all use another means of "chat" communique?

I've been trying to get the channel going, but most people prefer the email
list.  You can catch me on IRC during work hours most days (EST).  My nick
is "nirvdrum".

-- 
Kevin


Re: questions

Posted by Eric Polino <al...@gmail.com>.
On Feb 13, 2008 3:12 PM, Kevin Menard <km...@servprise.com> wrote:
> On 2/13/08 2:35 PM, "Eric Polino" <al...@gmail.com> wrote:
>
> >
> > I've been doing stuff like this already, that is simple.  I'm talking
> > about doing stuff like...
> >
>
> Apologies.  I wasn't trying to insult you or anything.  I guess since you
> said you had only been using Cayenne for a few weeks, I was assuming basic
> questions of functionality.  I think Michael and Andrus have gotten you on
> the right track now, though.
>

Oh, no insult incured.  I'm appreciating the help you guys are giving
me, I'm in no position to complain.  I've tried hopping into #cayenne
on freenode and there's nobody there?  Is there another channel? or do
you all use another means of "chat" communique?
> --
> Kevin
>
>



-- 
"None are more hopelessly enslaved than those who falsely believe they
are free."
                                      --Goethe

"Freedom is living without government coercion."
                   --Ron Paul (www.ronpaul2008.com)

Re: questions

Posted by Kevin Menard <km...@servprise.com>.
On 2/13/08 2:35 PM, "Eric Polino" <al...@gmail.com> wrote:

> 
> I've been doing stuff like this already, that is simple.  I'm talking
> about doing stuff like...
> 

Apologies.  I wasn't trying to insult you or anything.  I guess since you
said you had only been using Cayenne for a few weeks, I was assuming basic
questions of functionality.  I think Michael and Andrus have gotten you on
the right track now, though.

-- 
Kevin


Re: questions

Posted by Eric Polino <al...@gmail.com>.
On Feb 13, 2008 2:26 PM, Kevin Menard <km...@servprise.com> wrote:
> On 2/13/08 2:04 PM, "Eric Polino" <al...@gmail.com> wrote:
>
>
> >>> 2.  I've read in many places how Cayenne deals with joins for you, but
> >>> nowhere do I see an example of how its done.  Here's an example
> >>> problem I'm dealing with.
> >>>
> >>> Schema:
> >>>
> >>> Reservation (n to 1) Site
> >>> Site (n to 1)
> >>> Site contains a type field
> >>>
> >>> I want to get all the Reservations of a given for a given type of
> >>> site.  In sql I would execute something like, "select * from
> >>> Reservation, Site, SiteType where SiteType.Name='Something' and
> >>> Site.typeID = SiteType.id and Reservation.siteID = Site.id"
> >>>
> >>> Short of writing an parameterized sql string and executing that in my
> >>> code, how do I do that with Cayenne?  I hope I explained all that well
> >>> enough...make sense?
> >>
> >> Just map the relationships in the modeler.  Cayenne takes care of the SQL
> >> for you.  The following guide should help you get started with the modeler:
> >>
> >> http://cayenne.apache.org/doc20/tutorial-starting-mapping-project.html
> >
> > I've been using the modeler and the relationships are setup, I just
> > don't know how to do queries based on those relationship...ie the one
> > mentioned above
>
> Sorry.  I glossed over the original question.
>
> You want to use a qualifier expression:
>
> http://cayenne.apache.org/doc/qualifier-expressions.html
>
> Expression factory is the easiest way to do this:
>
> http://cayenne.apache.org/doc/api/org/apache/cayenne/exp/ExpressionFactory.h
> tml
>
> So, you may have something like:
>
> Query q = new SelectQuery(Site.class,
> ExpressionFactory.matchDbExp(Site.NAME, "Something");
>
> List<Site> sites = context.performQuery(q);
>
> for (Site s :sites)
> {
>     System.out.println(s.getReservation().toString();
> }
>

I've been doing stuff like this already, that is simple.  I'm talking
about doing stuff like...

Query q = new SelectQuery(Reservation.class,
ExpressionFactory(SiteType.NAME, "Something"));
List<Reservation> res = ...

This would select all the Reservations to a Site of SiteType "Something".

> You'll have to make substitutions as appropriate for your actual code.
>
> --
> Kevin
>
>



-- 
"None are more hopelessly enslaved than those who falsely believe they
are free."
                                      --Goethe

"Freedom is living without government coercion."
                   --Ron Paul (www.ronpaul2008.com)

Re: questions

Posted by Eric Polino <al...@gmail.com>.
On Feb 13, 2008 2:23 PM, Michael Gentry <bl...@gmail.com> wrote:
> Eric,
>
> This example is old (Cayenne 1.2):
>
> http://cwiki.apache.org/CAY/cayenne-in-motion.html

I think this is what I was looking for, I'll try doing what it talks about.

>
> But it pretty much applies to Cayenne 2.0 and 3.0, too.  Look at that
> page and do a search/find for "expression".  There are many examples
> of creating query expressions using joins in that example.  Let us
> know if you continue to have questions.
>
> Thanks,
>
> /dev/mrg
>
>
>
> On Feb 13, 2008 2:04 PM, Eric Polino <al...@gmail.com> wrote:
> > I've been using the modeler and the relationships are setup, I just
> > don't know how to do queries based on those relationship...ie the one
> > mentioned above
>



-- 
"None are more hopelessly enslaved than those who falsely believe they
are free."
                                      --Goethe

"Freedom is living without government coercion."
                   --Ron Paul (www.ronpaul2008.com)

Re: questions

Posted by Michael Gentry <bl...@gmail.com>.
Eric,

This example is old (Cayenne 1.2):

http://cwiki.apache.org/CAY/cayenne-in-motion.html

But it pretty much applies to Cayenne 2.0 and 3.0, too.  Look at that
page and do a search/find for "expression".  There are many examples
of creating query expressions using joins in that example.  Let us
know if you continue to have questions.

Thanks,

/dev/mrg


On Feb 13, 2008 2:04 PM, Eric Polino <al...@gmail.com> wrote:
> I've been using the modeler and the relationships are setup, I just
> don't know how to do queries based on those relationship...ie the one
> mentioned above

Re: questions

Posted by Kevin Menard <km...@servprise.com>.
On 2/13/08 2:04 PM, "Eric Polino" <al...@gmail.com> wrote:


>>> 2.  I've read in many places how Cayenne deals with joins for you, but
>>> nowhere do I see an example of how its done.  Here's an example
>>> problem I'm dealing with.
>>> 
>>> Schema:
>>> 
>>> Reservation (n to 1) Site
>>> Site (n to 1)
>>> Site contains a type field
>>> 
>>> I want to get all the Reservations of a given for a given type of
>>> site.  In sql I would execute something like, "select * from
>>> Reservation, Site, SiteType where SiteType.Name='Something' and
>>> Site.typeID = SiteType.id and Reservation.siteID = Site.id"
>>> 
>>> Short of writing an parameterized sql string and executing that in my
>>> code, how do I do that with Cayenne?  I hope I explained all that well
>>> enough...make sense?
>> 
>> Just map the relationships in the modeler.  Cayenne takes care of the SQL
>> for you.  The following guide should help you get started with the modeler:
>> 
>> http://cayenne.apache.org/doc20/tutorial-starting-mapping-project.html
> 
> I've been using the modeler and the relationships are setup, I just
> don't know how to do queries based on those relationship...ie the one
> mentioned above

Sorry.  I glossed over the original question.

You want to use a qualifier expression:

http://cayenne.apache.org/doc/qualifier-expressions.html

Expression factory is the easiest way to do this:

http://cayenne.apache.org/doc/api/org/apache/cayenne/exp/ExpressionFactory.h
tml

So, you may have something like:

Query q = new SelectQuery(Site.class,
ExpressionFactory.matchDbExp(Site.NAME, "Something");

List<Site> sites = context.performQuery(q);

for (Site s :sites)
{ 
    System.out.println(s.getReservation().toString();
}


You'll have to make substitutions as appropriate for your actual code.

--  
Kevin


Re: questions

Posted by Eric Polino <al...@gmail.com>.
On Feb 13, 2008 1:57 PM, Kevin Menard <km...@servprise.com> wrote:
> Comments in-line.
>
>
> On 2/13/08 1:48 PM, "Eric Polino" <al...@gmail.com> wrote:
>
> > I've started using Cayenne a few weeks ago for our project,
> > www.campgroundautomation.com.  In the process of working with it, I've
> > come across a few things I don't understand.  After reading the API
> > and the UserGuide, can't find much to help me.
> >
> > 1.  How do I count the rows of a query, without actually doing a full
> > blown SelectQuery and counting the elements?  ie: select count(*) from
> > table;
>
> Various aggregate queries come up rather frequently on the list.  I think
> someone posted a utility class at one time you could use.  Otherwise,
> SQLTemplate will get you there.
>
> http://cayenne.apache.org/doc/sqltemplate-basics.html
>
> > 2.  I've read in many places how Cayenne deals with joins for you, but
> > nowhere do I see an example of how its done.  Here's an example
> > problem I'm dealing with.
> >
> > Schema:
> >
> > Reservation (n to 1) Site
> > Site (n to 1)
> > Site contains a type field
> >
> > I want to get all the Reservations of a given for a given type of
> > site.  In sql I would execute something like, "select * from
> > Reservation, Site, SiteType where SiteType.Name='Something' and
> > Site.typeID = SiteType.id and Reservation.siteID = Site.id"
> >
> > Short of writing an parameterized sql string and executing that in my
> > code, how do I do that with Cayenne?  I hope I explained all that well
> > enough...make sense?
>
> Just map the relationships in the modeler.  Cayenne takes care of the SQL
> for you.  The following guide should help you get started with the modeler:
>
> http://cayenne.apache.org/doc20/tutorial-starting-mapping-project.html

I've been using the modeler and the relationships are setup, I just
don't know how to do queries based on those relationship...ie the one
mentioned above

>
> --
> Kevin
>
>



-- 
"None are more hopelessly enslaved than those who falsely believe they
are free."
                                      --Goethe

"Freedom is living without government coercion."
                   --Ron Paul (www.ronpaul2008.com)

Re: questions

Posted by Kevin Menard <km...@servprise.com>.
Comments in-line.


On 2/13/08 1:48 PM, "Eric Polino" <al...@gmail.com> wrote:

> I've started using Cayenne a few weeks ago for our project,
> www.campgroundautomation.com.  In the process of working with it, I've
> come across a few things I don't understand.  After reading the API
> and the UserGuide, can't find much to help me.
> 
> 1.  How do I count the rows of a query, without actually doing a full
> blown SelectQuery and counting the elements?  ie: select count(*) from
> table;

Various aggregate queries come up rather frequently on the list.  I think
someone posted a utility class at one time you could use.  Otherwise,
SQLTemplate will get you there.

http://cayenne.apache.org/doc/sqltemplate-basics.html

> 2.  I've read in many places how Cayenne deals with joins for you, but
> nowhere do I see an example of how its done.  Here's an example
> problem I'm dealing with.
> 
> Schema:
> 
> Reservation (n to 1) Site
> Site (n to 1)
> Site contains a type field
> 
> I want to get all the Reservations of a given for a given type of
> site.  In sql I would execute something like, "select * from
> Reservation, Site, SiteType where SiteType.Name='Something' and
> Site.typeID = SiteType.id and Reservation.siteID = Site.id"
> 
> Short of writing an parameterized sql string and executing that in my
> code, how do I do that with Cayenne?  I hope I explained all that well
> enough...make sense?

Just map the relationships in the modeler.  Cayenne takes care of the SQL
for you.  The following guide should help you get started with the modeler:

http://cayenne.apache.org/doc20/tutorial-starting-mapping-project.html

-- 
Kevin