views:

110

answers:

1

Hello,

I have the following mapping:

<hibernate-mapping package="server.modules.stats.data">
    <class name="User" table="user">
       <id name="id">
            <generator class="native"></generator>
       </id>
       <many-to-one name="address" column="addressId" unique="true" lazy="false" />
    </class>
    <class name="Address" table="address">
       <id name="id">
            <generator class="native"></generator>
       </id>
       <property name="street" />
    </class>
</hibernate-mapping>

How can I do a Criteria query to select all users living on some street?

That is create Criteria query for this SQL:

Select * from user join address on user.addressId = address.id where address.street='someStreet'
+1  A: 
String theStreet = "the_street_name";

DetachedCriteria dc = DetachedCriteria.forClass(User.class);
dc.createAlias("address", "addrAlias");
dc.add(Restrictions.eq("addrAlias.street", theStreet));
List<User> users = getHibernateTemplate().findByCriteria(dc);
Thierry