// 所以为了解决这个问题,我们使用离线条件查询对象,该对象的创建不需要session对象 DetachedCriteria detachedCriteria = DetachedCriteria.forClass...(User.class); // 使用离线条件查询对象,封装查询条件 detachedCriteria.add(Restrictions.eq("id", 100));...// 以后开发中常用DetachedCriteria
DetachedCriteria Hibernate2中,Criteria生命周期位于其宿主Session生命周期中,也就是说,由某个session创建的Criteria实例,一旦session销毁...Hibernate3中引入了DetachedCriteria,DetachedCriteria可以脱离session实例独立存在,这样,我们就可以将某些通用的Criteria查询条件进行抽离,每次使用时再与当前...DetachedCriteria deCriteria=DetachedCriteria.forClass(TUser.class); deCriteria.add(Expression.eq("name...的生存周期与session实例无关,我们可以独立创建DetachedCriteria实例,并在需要使用时与session相绑定,从而获得运行期Criteria实例。...DetachedCriteria也可以用于子查询表达: DetachedCriteria aveAge=DetachedCriteria.forClass(TUser.class); avgAge.setProjection
Criteria和DetachedCriteria的主要差别在于创建的形式不一样,Criteria是在线的,所以它是由HibernateSession进行创建的; 而DetachedCriteria是离线的...,创建时无需Session,DetachedCriteria提供了2个静态方法forClass(Class)或forEntityName(Name)进行DetachedCriteria实例的创建....Spring的框架提供了getHibernateTemplate().findByCriteria(detachedCriteria)方法,能够非常方便地依据DetachedCriteria来返回查询结果...DetachedCriteria query = DetachedCriteria.forClass(Cat.class) .add(Property.forName(“sex”).eq(‘F’));...DetachedCriteria avgWeight = DetachedCriteria.forClass(Cat.class) .setProjection(Property.forName(“weight
detachedCriteria; // 离线条件查询对象,封装查询条件 // List<?... getDetachedCriteria() { return detachedCriteria; } public void setDetachedCriteria(DetachedCriteria... detachedCriteria) { this.detachedCriteria = detachedCriteria; } public List<?... detachedCriteria = DetachedCriteria.forClass(Staff.class); // 创建离线条件查询对象 pageBean.setDetachedCriteria...(null); // 重置表和类的映射关系 detachedCriteria.setResultTransformer(DetachedCriteria.ROOT_ENTITY
criteria = DetachedCriteria.forClass(UserBean.class);// From UserBean where 1=1 if (!...类,因为hibernateTemplate.findByCriteria(DetachedCriteria criteria)方法必须传入DetachedCriteria, 关于DetachedCriteria...他们的用法差不多,只是DetachedCriteria相比Criteria少了一些方法。...(Name) 进行DetachedCriteria 的实例创建。...DetachedCriteria中去重复,并且将关联对象装配到主对象,用: criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) @
org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.hibernate.criterion.DetachedCriteria...--[endif]--> DetachedCriteria detachedCriteria = DetachedCriteria .forClass(InfoTab.class); detachedCriteria.addOrder...--[endif]--> PaginationSupport ps = userManager.findPageByCriteria(detachedCriteria, PaginationSupport.PAGESIZE
Demo.class); criteria.add(Restrictions.eq("param1", param1)); List demos = criteria.list(); // 推荐 DetachedCriteria...criteria = DetachedCriteria.forClass(Demo.class) .add(Restrictions.eq("param1", param1)); List<Demo...Demo.class); criteria.add(Restrictions.eq("param1", param1)); List demos = criteria.list(); // 推荐 DetachedCriteria...criteria = DetachedCriteria.forClass(Demo.class) .add(Restrictions.eq("param1", param1)); List<Demo
detachedCriteria = DetachedCriteria.forClass(Staff.class); // 向离线条件查询对象中封装条件 detachedCriteria.add...(Restrictions.eq("deltag", "0")); return staffDao.findByCriteria(detachedCriteria); } 第四步...:在IBaseDao中提供通用的条件查询方法 IBaseDao.java // 条件查询(不带分页) public List findByCriteria(DetachedCriteria...(DetachedCriteria detachedCriteria) { return this.getHibernateTemplate().findByCriteria(detachedCriteria... detachedCriteria = DetachedCriteria.forClass(Subarea.class); // 向离线条件查询对象中封装条件 // detachedCriteria.add
DetachedCriteria类和Criteria接口功能很类似,可以使用上述提到的方式(Criterion与Projection)设置查询条件,但两者的创建方式不同:Criteria必须由Session...对象创建,而DetachedCriteria创建时不需要Session对象。...因此DetachedCriteria可以在Session作用域之外构建,并添加一系列复杂条件,然后传递到具有Session环境的Dao方法中执行。...DetachedCriteria的出现实现了“条件构建”和“查询执行”的分离。...public static void main(String[] args) { DetachedCriteria cri = DetachedCriteria.forClass(Movie.class
List list = cri.list(); session.close(); return list; } 5、离线条件查询,如: //登录(用离线条件查询 DetachedCriteria...LoginUser6(UserPO up)throws Exception{ Session session = HibernateSessionFactory.getSession(); DetachedCriteria...dc = DetachedCriteria.forClass(UserPO.class); dc.add(Restrictions.eq("name", up.getName())); dc.add
detachedCriteria = null; 在BaseAction的构造方法中创建条件查询对象,并注入给PageBean对象 如下图所示: ?...对象转为JSON格式的数据的方法 this.writePageBean2Json(pageBean, new String[] {"currentPage", "pageSize", "detachedCriteria...this.writePageBean2Json(pageBean, new String[] {"currentPage", "pageSize", "detachedCriteria", "subareas... detachedCriteria2 = pageBean.getDetachedCriteria(); // QBC查询语句:当关联查询时,也即多表查询,需要创建别名,通过别名才可以访问关联对象的属性...= null) { // 创建别名,用于多表查询 detachedCriteria2.createAlias("region", "r");
即未启动的工作单) */ public List findListNotStart() { // 创建离线条件查询对象 DetachedCriteria... detachedCriteria = DetachedCriteria.forClass(Workordermanage.class); // 向离线条件查询对象中封装查询条件 ...detachedCriteria.add(Restrictions.eq("start", "0")); return workordermanageDao.findByCriteria...(detachedCriteria ); } 第二步:配置struts.xml <!
Customer customer : list) { System.out.println(customer); } tx.commit(); session.close(); } 1.2.6 离线条件查询: DetachedCriteria...@Test /** * 离线条件查询 */ publicvoid demo3(){ DetachedCriteria detachedCriteria = DetachedCriteria.forClass...(Customer.class); detachedCriteria.add(Restrictions.like("cname", "%马%")); detachedCriteria.add(Restrictions.gt...= HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); List list = detachedCriteria.getExecutableCriteria
Projections.rowCount()); Object o = criteria.uniqueResult(); System.out.println(o); 离线条件查询 脱离Session,添加条件 可以在外部提前使用DetachedCriteria...对象提交设置好条件 最后再绑定到session当中 public void lxTest(){ Session session = HibernateUtil.openSession(); DetachedCriteria...dc = DetachedCriteria.forClass(Course.class); dc.add(Restrictions.like("course_name","语%")); /
Serializable id); load(Class c,Serializable id); 查询多个 List find(String hql,Object… args); List findByCriteria(DetachedCriteria...dc); List findByCriteria(DetachedCriteria dc,int firstResult,int maxResults); List findByNamedQuery(...from Customer"); return list; } @Override public List findAllByQBC() { DetachedCriteria...criteria = DetachedCriteria.forClass(Customer.class); List list = (List) this.getHibernateTemplate
dCriteria = DetachedCriteria.forClass(Customer.class);//就相当于查询所有 from Customer; //2.拼装查询条件 //.../** * 查询所有客户,带条件 * @param dCriteria 查询条件 * @return */ List findAllCustomer(DetachedCriteria...Override @Transactional(propagation=Propagation.SUPPORTS,readOnly=true) public Page findAllCustomer(DetachedCriteria...list", list); super.put("list", list); */ // 推荐,带条件的查询,使用离线查询方式 // 创建离线条件的对象 DetachedCriteria...criteria = DetachedCriteria.forClass(Customer.class); // model已经封装了custName属性 String custName
finally { session.close(); sessionFactory.close(); } } 4.6 离线查询 DetachedCriteria...翻译为离线条件查询,他不不依赖 Session 来创建,所以 DetachedCriteria 可以被其他层使用条件封装,在交给 DAO 层查询,在 SSH 框架的整合上经常使用。...sessionFactory.openSession(); tx = session.beginTransaction(); //离线查询 DetachedCriteria...detachedCriteria = DetachedCriteria.forClass(Student.class); Criteria criteria = detachedCriteria.getExecutableCriteria
com.jinke.ssh.dao.CustomerDao; import com.jinke.ssh.domain.Customer; import org.hibernate.criterion.DetachedCriteria...return list; } @Override public List findAllByQBC() { //自带分页 DetachedCriteria...criteria = DetachedCriteria.forClass(Customer.class); List list = (List)
functionService.pageQuery(pageBean); String[] excludes = new String[] {"currentPage", "pageSize", "detachedCriteria...functionService.pageQuery(pageBean); String[] excludes = new String[] {"currentPage", "pageSize", "detachedCriteria...list = roleService.findAll(); String[] excludes = new String[] {"currentPage", "pageSize", "detachedCriteria...= functionService.findMenu(); String[] excludes = new String[] {"currentPage", "pageSize", "detachedCriteria
criteria.uniqueResult(); System.out.println(num); tx.commit(); } 1.4.6离线条件查询(SSH)---DetachedCriteria...Test /** * 离线条件查询 */ publicvoid demo6(){ DetachedCriteriadetachedCriteria = DetachedCriteria.forClass...(Customer.class); detachedCriteria.add(Restrictions.like("cust_name", "李%")); Session...HibernateUtils.getCurrentSession(); Transaction transaction =session.beginTransaction(); Criteria criteria =detachedCriteria.getExecutableCriteria
领取专属 10元无门槛券
手把手带您无忧上云