2010-03-10 24 views
0

我有一个hibernate过滤器,需要使用数据库中的两个不同列进行评估。我有另一个包含这两个字段的现有hibernate对象,我希望能够将另一个hibernate对象传入session.enableFilter.setParameter()调用,而不是单独传递其中包含的两个值。你可以在Hibernate过滤器中使用自定义对象吗?

具体来说,我想将这段代码:

session 
    .enableFilter("inLocation") 
    .setParameter("traversalLeft", 5) 
    .setParameter("traversalRight", 10); 

与此代码:

session 
    .enableFilter("inLocation") 
    .setParameter("location", locationHibernateObject) 

更好地隔离excactly什么过滤器的需求。

但是,当我尝试配置像过滤器:

@FilterDef(name="inLocation", [email protected](name="location", type="com.example.Location")) 
@Filters({ 
    @Filter(name="inLocation", condition="current_location_id in (select location.id from location where location.traversal_left between :location.traversalLeft+1 and :location.traversalRight)") 
}) 
public class ClassToFilter { 

我得到试图调用enableFilter()的错误

这是东西,甚至有可能?我究竟做错了什么?

回答

1

我遇到了同样的问题。

我使用hibernate-annotations-3.4.0.GA,和我的过滤器和自定义类型在一个package-info.java文件。使用该设置,问题似乎在:

  • org.hibernate.cfg.AnnotationBinder#bindPackage(),其中过滤器正在处理之前,而不是自定义类型之后。
  • org.hibernate.cfg.AnnotationBinder#bindFilterDef(),它根本没有尝试发现自定义类型。

我改变的方法调用顺序,并在bindFilterDef()替换为呼叫:

 
    private static void bindFilterDef(FilterDef defAnn, ExtendedMappings mappings) { 
     Map params = new HashMap(); 
     for (ParamDef param : defAnn.parameters()) { 
      /////////////////////////////////////////////////////////////////////////////////////////////////////////// 
      // support for custom types in filters 
      params.put(param.name(), getType(param.type(), mappings)); 
     } 
     FilterDefinition def = new FilterDefinition(defAnn.name(), defAnn.defaultCondition(), params); 
     log.info("Binding filter definition: {}", def.getFilterName()); 
     mappings.addFilterDefinition(def); 
    } 

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    // support for custom types in filters 
    private static org.hibernate.type.Type getType(String paramType, ExtendedMappings mappings) { 
     org.hibernate.type.Type heuristicType = TypeFactory.heuristicType(paramType); 
     if (heuristicType == null) { 
      org.hibernate.mapping.TypeDef typeDef = mappings.getTypeDef(paramType); 
      if (typeDef != null) { 
       heuristicType = TypeFactory.heuristicType(typeDef.getTypeClass(), typeDef.getParameters()); 
      } 
     } 
     log.debug("for param type: {} parameter heuristic type : {}", paramType, heuristicType); 
     return heuristicType; 
    } 

当然,那我只好从头开始构建的罐子,但变化似乎解决问题。

然而,在休眠3.5,注解类捆绑内部hibernate3.jar里,所以更多的工作可能是必需的,因为一切都要从头开始构建。

+0

这绝对会让它听起来像一个休眠问题,而不仅仅是一个我错过的配置。 – 2010-04-29 20:12:12

相关问题