http://blog.csdn.net/cuiran/article/details/6324083
补充:
百度:criteria:
http://baike.baidu.com/link?url=49PcEgB_kMdEfRGg-aBS_YqEKWEvEwfTZmzHk42CLX1vinmvR7bOGS3tAnFxz6BS2BrBpP1zLsTbSZx0fo35cq
收集:
1 获得count:
public Long findCount(String vendorId) {
DetachedCriteria criteria = DetachedCriteria.forClass(Inventory.class)
.setProjection(Projections.rowCount())
.add(Restrictions.eq(VENDORID, vendorId));
return Long.parseLong(getHibernateTemplate().findByCriteria(criteria).get(0).toString());
}(2)
public int countAccessCodes(String vid) {
StringBuffer query = new StringBuffer("SELECT COUNT(*) FROM ").append(Accesscodes.class.getName()).append(" where vid='").append(vid).append("'");
List rs = (List) getHibernateTemplate().find(query.toString());
if(rs.isEmpty())
return 0;
else
return Integer.parseInt(rs.get(0).toString());
}
2 分页:
List<Inventory> inventories = (List<Inventory>) getHibernateTemplate()
.execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
return session.createQuery(hql.toString()).setFirstResult(firstRow)
.setMaxResults(pageSize).list();
}});3 查询某个字段:
public List<String> findPropvalues(String vendorId, int propnumber) {
return (List<String>)getHibernateTemplate().findByCriteria(
DetachedCriteria.forClass(Properties.class)
.add(Restrictions.eq(VID, vendorId))
.add(Restrictions.eq(PROPNUMBER, propnumber))
.addOrder(Order.asc(PROPVALUE))
.setProjection(Projections.distinct(Projections.property(PROPVALUE)))
);
}4 模糊查询like
public List<Orderitemattribs> findByImage(Long orderdataid, Long orderitemid){
return (List<Orderitemattribs>)getHibernateTemplate().findByCriteria(
DetachedCriteria.forClass(Orderitemattribs.class)
.add(Restrictions.eq("orderid", orderdataid))
.add(Restrictions.eq("orderitemid", orderitemid))
.add(Restrictions.like("attname", "%Image%"))
);
}
5 not in :
public List<A> find(String vendorId, boolean isPerServerMode, boolean showGiftCardCert) {
DetachedCriteria query = DetachedCriteria.forClass(A.class)
.add((!isPerServerMode)?Restrictions.eq(VID, vendorId):Restrictions.or(Restrictions.eq(VID, vendorId), Restrictions.isNull(VID))
);
if(!showGiftCardCert) {
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(B.class)
.add(Restrictions.eq(VID, vendorId))
.add(Restrictions.isNotNull(GIFT_CERT_ID))
.setProjection(Projections.property(GIFT_CERT_ID));
query.add(Subqueries.propertyNotIn(GIFT_CERT_ID, detachedCriteria));
}
return (List<A>) getHibernateTemplate().findByCriteria(query);
}
6 查询条件in (id,id,id,id)
public List<Properties> findProperties(String vendorId, String propName,
List<Long> itemIds) {
if (itemIds.isEmpty())
return new ArrayList<Properties>();
DetachedCriteria criteria = DetachedCriteria.forClass(Properties.class)
.add(Restrictions.eq(VID, vendorId))
.add(Restrictions.eq(PROP_NAME, propName));
Disjunction disjunction = Restrictions.disjunction();
for (Long itemId : itemIds)
if (itemId != null)
disjunction.add(Restrictions.eq(IID, itemId)); // 这里写根据条件list添加
criteria.add(disjunction);
return getHibernateTemplate().findByCriteria(criteria);
}
7 多表查询:
(1)多对一:如图1,实体类
如图2:语法:
8 使用hsql
public List<SuggestionAdminGroup> findAdminGroup(final String vid,final Long adminid){
return (List<SuggestionAdminGroup>)getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String sql = "SELECT s FROM "+SuggestionAdminGroup.class.getName()+" s,"+SuggestionGroup.class.getName()+" g where " +
"s.adminId="+adminid+" and g.vid='"+vid+"' and g.suggestionGroupId=s.suggestionGroupId"; // hsql
Query query = session.createQuery(sql);
return query.list();
}
});
}
hibernate_Restrictions用法
最新推荐文章于 2019-09-05 23:59:39 发布