FAQ
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

Search Discussions

  • Hugi Thordarson at Sep 26, 2015 at 10:59 am
    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 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
  • Dipesh Jain at Sep 26, 2015 at 1:20 pm
    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.
  • Hugi Thordarson at Sep 26, 2015 at 3:23 pm
    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 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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupuser @
categoriescayenne
postedSep 26, '15 at 7:04a
activeSep 26, '15 at 3:23p
posts4
users2
websitecayenne.apache.org

2 users in discussion

Dipesh Jain: 2 posts Hugi Thordarson: 2 posts

People

Translate

site design / logo © 2021 Grokbase