Hibernate离线查询(DetachedCriteria)

----------------------------------------dao---------------------------------------------------------------------------

//离线查询
 @SuppressWarnings("unchecked")
 public List detachedCriteria(final DetachedCriteria detachedCriteria)
 {
  return this.getHibernateTemplate().execute(new HibernateCallback()
  
  {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    //构造Criteria,detachedCriteria与session关联下
    Criteria criteria = detachedCriteria.getExecutableCriteria(session);
    return criteria.list();
   }
  });
 }


-------------------------------------service-----------------------------------------------------------------------------

@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
 public List detachedCriteria(final DetachedCriteria detachedCriteria)
 {
  return hibernateDao.detachedCriteria(detachedCriteria);
 }


--------------------------------------Action--------------------------------------------------------------------------------

/**
  * 离线查询
  */
 @SuppressWarnings("unchecked")
 public String detachedCriteria()
 {
  //构造DetachedCriteria对象
  DetachedCriteria dc = DetachedCriteria.forClass(User.class);
  
  //构造条件
  if(null!=user){
   String userName = user.getUserName();
   if(userName!=null && !"".equals(userName)){
       dc.add(Restrictions.eq("userName", user.getUserName()));
   }
   String passWord = user.getPassWord();
   if(passWord!=null && !"".equals(passWord)){
    dc.add(Restrictions.eq("passWord", user.getPassWord()));
   }
  }
  //查询
  List list = springService.detachedCriteria(dc);
  System.out.println(list.size());
  for(int i=0;i

你可能感兴趣的:(Hibernate)