Right them, criteria where the subselect clause includes comparison with tables in the principal select:
MeterPoint contains a reference to Location, which contains a reference to an Address. This criteria finds all meter points that are named after the city they're in, so long as it begins with W:
Code:
DefaultMeterPointFactory fact = (DefaultMeterPointFactory) lookup(MeterPointFactory.ROLE);
Session session = fact.session();
DetachedCriteria sub1 = DetachedCriteria.forClass(AddressImpl.class, "addr");
sub1.setProjection(Projections.property("id"));
sub1.add(Property.forName("addr.City").eqProperty("mp.Name"));
Criteria crit = session.createCriteria(MeterPointImpl.class, "mp");
crit.add(Restrictions.like("Name", "W%"));
crit.createAlias("Location", "mploc");
crit.createAlias("mploc.Address", "mpaddr");
crit.add(Subqueries.propertyEq("mpaddr.id", sub1));
This generates the following SQL:
Code:
select this_.MeterPointID as MeterPoi1_6_,
this_.MeterPointName as MeterPoi2_14_6_,
this_.RetailerNote as Retailer3_14_6_,
this_.LocationID as LocationID14_6_,
this_.ManagementCompanyID as Manageme5_14_6_,
mploc1_.LocationID as LocationID0_,
mploc1_.Locator1 as Locator2_4_0_,
mploc1_.AddressID as AddressID4_0_,
mpaddr2_.AddressID as AddressID1_,
mpaddr2_.Number as Number3_1_,
mpaddr2_.Street as Street3_1_,
mpaddr2_.Suburb as Suburb3_1_,
mpaddr2_.City as City3_1_,
mpaddr2_.State as State3_1_,
mpaddr2_.PostalCode as PostalCode3_1_,
mpaddr2_.Country as Country3_1_
inner join Location mploc1_ on this_.LocationID=mploc1_.LocationID
inner join Address mpaddr2_ on mploc1_.AddressID=mpaddr2_.AddressID
where this_.MeterPointName like ?
and mpaddr2_.AddressID = (select this0__.AddressID as y0_ from Address this0__
where this0__.City=this_.MeterPointName)
It tested it this time, it works. this0__ is the Address in the subquery, this_ is the meter point from the main query, and they're compared in the subquery. Is that what you wanted?