2010-03-10 26 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

相關問題