服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

hibernate3的detachedcriteria支持

  hibernate3支持detachedcriteria,这是一个非常有意义的特性!我们知道,在常规的web编程中,有大量的动态条件查询,即用户在网页上面自由选择某些条件,程序根据用户的选择条件,动态生成sql语句,进行查询。

  针对这种需求,对于分层应用程序来说,web层需要传递一个查询的条件列表给业务层对象,业务层对象获得这个条件列表之后,然后依次取出条件,构造查询语句。这里的一个难点是条件列表用什么来构造?传统上使用map,但是这种方式缺陷很大,map可以传递的信息非常有限,只能传递name和value,无法传递究竟要做怎样的条件运算,究竟是大于,小于,like,还是其它的什么,业务层对象必须确切掌握每条entry的隐含条件。因此一旦隐含条件改变,业务层对象的查询构造算法必须相应修改,但是这种查询条件的改变是隐式约定的,而不是程序代码约束的,因此非常容易出错。

  detachedcriteria可以解决这个问题,即在web层,程序员使用detachedcriteria来构造查询条件,然后将这个detachedcriteria作为方法调用参数传递给业务层对象。而业务层对象获得detachedcriteria之后,可以在session范围内直接构造criteria,进行查询。就此,查询语句的构造完全被搬离到web层实现,而业务层则只负责完成持久化和查询的封装即可,与查询条件构造完全解耦,非常完美!这恐怕也是以前很多企图在web层代码中构造hql语句的人想实现的梦想吧!

  示例代码片段如下:

  web层程序构造查询条件:

  java代码:

detachedcriteria detachedcriteria = detachedcriteria.forclass(department.class);
detachedcriteria.add(restrictions.eq("name", "department")).createalias("employees", "e").add(restrictions.gt(("e.age"), new integer(20)));

  department和employee是一对多关联,查询条件为:

  名称是“department”开发部门;
  部门里面的雇员年龄大于20岁;

  业务层对象使用该条件执行查询:

  java代码:

detachedcriteria.getexecutablecriteria(session).list();

  最大的意义在于,业务层代码是固定不变的,所有查询条件的构造都在web层完成,业务层只负责在session内执行之。这样代码就可放之四海而皆准,都无须修改了。

  然而spring和hibernate3的detachedcriteria有不兼容的问题,因此在spring环境下面使用hibernate3需要注意:

  spring的hibernatetemplate提供了hibernate的完美封装,即通过匿名类实现回调,来保证session的自动资源管理和事务的管理。其中核心方法是:

  java代码:

hibernatetemplate.execute(new hibernatecallback() {
 public object doinhibernate(session session) throws hibernateexception {
  ....
 }
}

  回调方法提供了session作为参数,有了session,就可以自由的使用hibernate api编程了。使用了spring的之后,代码修改如下:

  web层代码:

  java代码:

detachedcriteria detachedcriteria = detachedcriteria.forclass(department.class);
detachedcriteria.createalias("employees", "e").add(restrictions.eq("name", "department")).add(restrictions.gt(("e.age"), new integer(20)));
departmentmanager.findbycriteria(detachedcriteria);

  构造detachedcriteria,作为参数传递给departmentmanager

  业务层代码使用spring,departmentmanager的findbycriteria如下:

  java代码:

public list findbycriteria(final detachedcriteria detachedcriteria) {
 return (list) gethibernatetemplate().execute(new hibernatecallback() {
  public object doinhibernate(session session) throws hibernateexception {
   criteria criteria = detachedcriteria.getexecutablecriteria(session);
   return criteria.list();
  }
 });
}

  实际上也就是:

  java代码:

criteria criteria = detachedcriteria.getexecutablecriteria(session);
return criteria.list();

  而已

  但是该程序代码执行,会抛出强制类型转换异常!

  我跟踪了一下spring和hibernate源代码,原因如下:

  spring的hibernatetemplate的execute方法提供的回调接口具有session作为参数,但是实际上,默认情况下,hibernatetemplate传递给回调接口的session并不是org.hibernate.impl.sessionimpl类,而是sessionimpl类的一个proxy类。之所以替换成为一个proxy类,hibernatetemplate的注释说明,proxy提供了一些额外的功能,包括自动设置cachable,transaction的超时时间,session资源的更积极的关闭等等。

  java代码:

private boolean exposenativesession = false;
...

  execute方法内部:

session sessiontoexpose = (exposenativesession ? session : createsessionproxy(session));

  但是遗憾的是,hibernate的detachedcriteria的setexecutablecriteria方法却要求将session参数强制转为sessionimpl,但是spring传过来的却是一个proxy类,因此就报错了。

  java代码:

public criteria getexecutablecriteria(session session) {
 impl.setsession( (sessionimpl) session ); // 要求sessionimpl,spring传递的是proxy
 return impl;
}

  解决方法,禁止spring的hibernatetemplate传递proxy类,强制要求它传递真实的sessionimpl类,即给exexute方法增加一个参数,提供参数为true,如下:

  java代码:

public list findbycriteria(final detachedcriteria detachedcriteria) {
 return (list) gethibernatetemplate().execute(new hibernatecallback() {
  public object doinhibernate(session session) throws hibernateexception {
   criteria criteria = detachedcriteria.getexecutablecriteria(session);
   return criteria.list();
  }
 }, true);
}

扫描关注微信公众号