-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Self-join vs. Subquery
PostPosted: Thu Jan 08, 2009 9:48 am 
Newbie

Joined: Thu Jan 08, 2009 8:21 am
Posts: 5
Hibernate version: 3.1.3

Hello all,

I'ld like to hear your opinions and/or suggestions on the following situations. Thanks a lot in advance!


1.) select * from customer x, customer y WHERE x.lastname = y.lastname AND y.id = '4711'

I would like to get all customers from a table "customer" which have the same lastname as the customer
with the id "4711".

Since a self-join is not supported by Hibernate Criteria (that's what I read - is this realy true?),
I helped myself out with a subquery:


Code:
        DetachedCriteria tempSubquery = DetachedCriteria.forClass(Customer.class);
        tempSubquery.add(Restrictions.eq("id", "4711"));
        tempSubquery.setProjection(Projections.max("lastname"));

        Criteria tempCriteria = tempSession.createCriteria(Customer.class);
        tempCriteria.add(Subqueries.propertyEq("lastname", tempSubquery));



Maybe not the loveliest way to solve the problem, but it works...


2.) select * from customer x, customer y WHERE x.lastname = y.lastname AND x.firstname = y.firstname AND y.id = '4711'

I would like to get all customers from a table "customer" which have the same last- and firstname as
the customer with the id "4711".

Is there any way to solve this problem with Criterias?

My idea was something like this (code will not work!):


Code:
        DetachedCriteria tempSubquery = DetachedCriteria.forClass(Customer.class);
        tempSubquery.add(Restrictions.eq("id", "4711"));

        ProjectionList tempProjectionList = Projections.projectionList();
        tempProjectionList.add(Projections.property("lastname"));
        tempProjectionList.add(Projections.property("firstname"));

        tempSubquery.setProjection(tempProjectionList);

        Criteria tempCriteria = tempSession.createCriteria(Customer.class);
        tempCriteria.add(Subqueries.propertyEq("lastname", tempSubquery.getColumn(0)));
        tempCriteria.add(Subqueries.propertyEq("firstname", tempSubquery.getColumn(1)));



As I wrote before - the code will not work. There is no "getColumn". But what I am looking for is a
way to access the second column gotten from the subquery...


Any ideas?


Thanks again!
Stephan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 1:44 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
For 1.) i would never do a join but always a subquery/subselect

even only 100 customers would mean that 100x100 have to be joined and then reduced by the matching names and the id. Though any decent DBMS would pick the customer with id 4711 first i guess.

ad 2.) Subquery again, just leave out the Projection and get the Customer with .uniqueResult
Then just use .getLastname() and .getFirstname for the main query.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.