uniqueResult()
当确定返回的实例只有一个或者null时 用uniqueResult()方法
代码示例:
Session session = factory.openSession();
session.beginTransaction(); Query query = session.createQuery("from Student s where s.id=?");
query.setString(0, "2");
Student student = (Student)query.uniqueResult();
HQL查询
查询参数格式“:aNamedParameter
” ;参数赋值 .setParameter("aNamedParameter",parameterVlaue).
String queryString =
"select c " +
"from Customer c " +
"where c.name = :name " +
" or c.nickName = :nickName "; // HQL List customers = session.createQuery( queryString )
.setParameter( "name", theNameOfInterest )
.setParameter( "nickName", theNameOfnickName )
.list();
Criteria查询
Session session = getSession();
Criteria cri = session.createCriteria(Account.class);
//判断等于
cri.add(Restrictions.eq("mobile", mobile));
//大于
cri.add(Restrictions.gt("status",
AccountStatus.Invalid.getStatus()));
//小于
cri.add(Restrictions.lt("count", 10));
List<Account> list = cri.list();