表关联时,外键关联非主键id时-hbm.xml配置:
(由于hibernate默认为关联主键查询,故需要配置相关hql语句的属性)
<many-to-one name="areas" class="com.hnqy.entity.Areas" fetch="select" property-ref="areaid" foreign-key="areaid">
<column name="area" />
</many-to-one>
其中:foreign-key为主表字段,property-ref为类属性
关于
hibernateTemplate.get(Ordermanager.class, id);返回为类对象
hibernateTemplate.find("from Shoppingitems where orderid = ? ", id);返回为List结合
关于查询分页的 DaoImpl
public List<Ordermanager> findByPage( String orderNumber,String userAccount,String sendName,Integer orderState , int currentPage, int pageSize) {
Session session=hibernateTemplate.getSessionFactory().getCurrentSession();
List Hqllist=new ArrayList<>();
StringBuffer hql=new StringBuffer("from Ordermanager where 1=1 ");
if(orderNumber!=null&& !"".equals(orderNumber)){
hql.append(" and orderNumber= ? ");
Hqllist.add(orderNumber);
}
if(userAccount!=null&& !"".equals(userAccount)){
hql.append(" and userAccount= ? ");
Hqllist.add(userAccount);
}
if(sendName!=null&& !"".equals(sendName)){
hql.append(" and sendName= ? ");
Hqllist.add(sendName);
}
if(orderState!=null&& !"".equals(orderState)){
hql.append(" and orderState= ? ");
Hqllist.add(orderState);
}
Query q=session.createQuery(hql.toString());
for (int i = 0; i < Hqllist.size(); i++) {
q.setParameter(i, Hqllist.get(i));
}
q.setFirstResult((currentPage-1)*pageSize).setMaxResults(pageSize);
List<Ordermanager> list1=q.list();
return list1;
}
外键关联非主键id时-hbm.xml配置,及其分页查询DaoImpl
原文:http://www.cnblogs.com/hua-show/p/7252707.html