You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Dipesh Jain <di...@ivgroup.in> on 2015/09/26 09:04:05 UTC
How to use Join Queries
I have searched example for SQL joins using apache cayenne but I found
nothing. How can i use selectQuery with Expression for JOINs or I have to
use SQLTemplete or EJBQL Query.
Like I have a query -
" Select p.name, a.address from Person p RIGHT OUTER JOIN Address a ON
p.personId = a.personId where p.name LIKE 'Deep%' AND p.age > 18 "
then to write it in cayenne ?
--
Thanks and Regards
Deepesh Jain
Re: How to use Join Queries
Posted by Hugi Thordarson <hu...@karlmenn.is>.
If everything is modeled correctly, your “Person” class should have an “addresses” method that will return a List containing all related addresses.
- hugi
> On 26. sep. 2015, at 13:20, Dipesh Jain <di...@ivgroup.in> wrote:
>
> Hii Hugi
> I am using Cayenne 3.1. You are assuming correctly. I do have mapped table
> ids but not with foreign key constraint, so I can not use foreign key
> constraint for mapping. And in this code where are you comparing two
> entities there is just person class hows it using join with address table.
Re: How to use Join Queries
Posted by Dipesh Jain <di...@ivgroup.in>.
Hii Hugi
I am using Cayenne 3.1. You are assuming correctly. I do have mapped table
ids but not with foreign key constraint, so I can not use foreign key
constraint for mapping. And in this code where are you comparing two
entities there is just person class hows it using join with address table.
Re: How to use Join Queries
Posted by Hugi Thordarson <hu...@karlmenn.is>.
Hi Deepesh!
Disclaimer: Note my answer applies to Cayenne version 4.0 (it’s the only version I know and I don’t know the older APIs).
I’m assuming you’re new to Cayenne and inherited an existing project.
Cayenne maps a database to a java object graph, and using Cayenne you'll usually have a one-to-one mapping of a database table to an “entity" (usually mapped to a java class). Entities also contain information about how they relate (are joined), those are called “relationships” and are represented by java methods on the entity classes (this will all sound very familiar if you’ve ever used an ORM before).
So: You probably (hopefully) already have entities (java classes) called “Person” and “Address”, joined by relationships called “people” and “addresses”. Assuming this is correct, the query you describe can be performed with:
---
SelectQuery<Person> query = new SelectQuery<>( Person.class );
query.setQualifier( Person.NAME.outer().eq( “Deep%” ).and( Person.AGE.outer().gt( 18 ) );
List<Person> peopleNamedDeepAndOlderThan18 = someObjectContext.select( query );
—
Note the type safety. It’s a thing of beauty, really.
Cheers,
- hugi
> On 26. sep. 2015, at 07:04, Dipesh Jain <di...@ivgroup.in> wrote:
>
> I have searched example for SQL joins using apache cayenne but I found
> nothing. How can i use selectQuery with Expression for JOINs or I have to
> use SQLTemplete or EJBQL Query.
>
> Like I have a query -
> " Select p.name, a.address from Person p RIGHT OUTER JOIN Address a ON
> p.personId = a.personId where p.name LIKE 'Deep%' AND p.age > 18 "
>
> then to write it in cayenne ?
>
> --
> Thanks and Regards
> Deepesh Jain