Issue with Projections and Alias Names fro Joining same table with different Alias

1,822 views
Skip to first unread message

Jeffrey Williams

unread,
Feb 13, 2012, 3:10:40 PM2/13/12
to quer...@googlegroups.com
I have the following QueryDSL query.

JPAQuery query = new JPAQuery(em);

private QSalesDeal deal = QSalesDeal.salesDeal;

private QClientUser primarySalesperon = QClientUser.clientUser;

private QClientUser secondarySalesperon = QClientUser.clientUser;

query = query.from(deal);

query.innerJoin(deal.salesPerson1, primarySalesperon);

query.leftJoin(deal.salesPerson2, secondarySalesperon);

List<GrossProfitLeadRecord> leadRecords = query.list(new QGrossProfitLeadRecord(deal.id,

lead.id,

deal.dealNumber,

deal.bookDate,

primarySalesperon.id,

primarySalesperon.firstName,

primarySalesperon.lastName,

secondarySalesperon.firstName,

secondarySalesperon.lastName));

It returns the following sql:

SELECT salesdeal0_.id              AS col_0_0_, 
       salesdeal0_.dealnumber      AS col_2_0_, 
       salesdeal0_.bookdate        AS col_3_0_, 
       clientuser5_.id             AS col_7_0_, 
       clientuser5_.firstname      AS col_8_0_, 
       clientuser5_.lastname       AS col_9_0_, 
       clientuser5_.firstname      AS col_10_0_, 
FROM   buzztrak3.deal salesdeal0_ 
       INNER JOIN buzztrak3.bzuser clientuser3_ ON salesdeal0_.salesperson1id = clientuser3_.id 
       LEFT OUTER JOIN buzztrak3.bzuser clientuser5_ N salesdeal0_.salesperson2id = clientuser5_.id 

You will notice in the select clause that the table references are incorrect and is only referencing the last table added with target. Is this a bug? known limitation or am I just simply missing something.

Timo Westkämper

unread,
Feb 13, 2012, 3:17:46 PM2/13/12
to quer...@googlegroups.com
Hi.

You can't use the same Querydsl expression to refer to different paths in a query.

Define the QClientUser instances like this instead:

  QClientUser primarySalesperson = new QClientUser("primarySalesperson");

  QClientUser secondarySalesperson = new QClientUser("secondarySalesperson");

Timo

--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com



Jeffrey Williams

unread,
Feb 13, 2012, 3:30:54 PM2/13/12
to quer...@googlegroups.com
Awesome that works.

Thanks for the quick reply. 

Can you provide a little more detail on how that differs to what I was doing. Our office has been following the documentation about how to do joins
query.from(cat)
    .innerJoin(cat.mate, mate)
    .leftJoin(cat.kittens, kitten)
    .list(cat);

Maybe you can add a section to the documentation about this case as I could not easily find anything. Thou I have been known for missing the obvious!

Timo Westkämper

unread,
Feb 13, 2012, 3:41:14 PM2/13/12
to quer...@googlegroups.com
Hi.

On Mon, Feb 13, 2012 at 10:30 PM, Jeffrey Williams <jeffxor....@gmail.com> wrote:
Awesome that works.

Thanks for the quick reply. 

Can you provide a little more detail on how that differs to what I was doing. Our office has been following the documentation about how to do joins
query.from(cat)
    .innerJoin(cat.mate, mate)
    .leftJoin(cat.kittens, kitten)
    .list(cat);
It was not clear from the documentation that cat, mate and kitten need to be distinct expressions.

e.g.

QCat cat = QCat.cat;
QCat mate = new QCat("mate");
QCate kitten = new QCat("kitten") ;

QCat.cat is just a static shortcut for new QCat("cat")
 
Maybe you can add a section to the documentation about this case as I could not easily find anything. Thou I have been known for missing the obvious!
Reply all
Reply to author
Forward
0 new messages