hibernate使用记录

1.执行SQL语句而非hql语句,getSession().createQuery(sql2) 执行的是hibernate语句;

Query query2 = this.onlineMonitorDao.getSession().createSQLQuery(sql2);
list = query2.list();
if (list != null&&list.size()>0) {
u1+=(Long)list.get(0);
}
return u1;

 

2.统计数目语句,在dao(public class BaseDao<T, ID extends Serializable> extends HibernateDao<T, ID>)中也可编写:

/**
* 当天城管办件处理情况
* @author lulj
* @param sql
* @return
*/
public List<Long> statCgOfToday(String sql) {
List<Long> result = this
.find(sql);
return result;
}

3.Criteria 的使用方法

for (Ccproblem ccproblem : problems) {
			Criteria criteria = ccproblemDao
					.getSession()
					.createCriteria(
							Ccproblem.class);
			Example example = Example
					.create(ccproblem);
			criteria.add(Restrictions
					.ne("bigtype",
							"006"));//
			criteria
					.add(Restrictions
							.ne(
									"problemsource",
									"8"));//
			criteria
					.addOrder(Order
							.desc("uploadtime"));
			criteria.add(example);
			resultCcproblems
					.addAll(criteria
							.list());
		}

  

 3.两张表关联,但附表只是做查询条件之用时

 Query q = workTaskDao.createSQLQuery(hql.toString()).addEntity(WorkTask.class);

 List<WorkTask> list=q.list();

List<WorkTaskForm> listss = WorkTaskConvertor.convertVoListToFormList(list);
pg.setResult(listss);

posted on 2017-06-01 14:56  决心觉新  阅读(111)  评论(0编辑  收藏  举报