2010-03-16 81 views
0

作出屬性查詢從連接表在Hibernate中我有以下映射:如何使用標準

<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> 

我怎樣才能做一個標準的查詢選擇居住在一些街頭的所有用戶?

就是這個SQL創造條件查詢:

Select * from user join address on user.addressId = address.id where address.street='someStreet' 

回答

2
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);