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