-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Filter not applied to sub-level elements
PostPosted: Sun Sep 11, 2005 9:47 am 
Newbie

Joined: Thu Sep 08, 2005 3:49 pm
Posts: 3
Location: Frankfurt, Germany
Hi !

I'd like to use Hibernate 3 filters for temporal data. The problem is: the filter only gets applied to an element if this element is the top-level element in a query or a load. The filter is NOT applied if the element is eagerly fetched or is lazily fetched by navigating a collection. Is this a bug or am I missing something here ?

I've tried to get this to work for some time now. I even discovered a post (as yet unanswered) by lovkiys on 30 Aug. who seems to have the same problem (title: "Hibernate 3 filters for a object graph loading").

The following example demonstrates the problem.
There are two classes: Parent and Child. Parent contains a collection (bag) of Child objects. I've inserted a single Parent with three Child objects into the database in a previous session. When I enable the filter and then load all Child objects from the database with the query "from Child" I get only one Child object. This is expected since the filter only matches one of the three children. Fine.
But when I load (or get or query) the Parent element it has in it's collection of child objects all three children ! I would have expected only one Child here as well. It is evident from the generated SQL (show_sql=true) that the filter clause is not included in this second case. But why not ?

Changing the fetching strategy from lazy to eager fetching didn't make any difference.

Help is very much appreciated.
Thanks, joak


Hibernate version:
3.0.5 (also tried 3.1beta2: same problem)

Mapping documents:
Code:
<hibernate-mapping>
   <class name="histest.Parent" table="PARENT">
      <id name="id">
         <column name="ID" not-null="true"/>
         <generator class="native" />
      </id>
      <property name="name" />
      <bag name="children" cascade="all" lazy="false">
         <key column="PARENT_ID" />
         <one-to-many class="histest.Child" />
      </bag>
   </class>
   <class name="histest.Child" table="CHILD">
      <id name="id">
         <column name="ID" not-null="true"/>
         <generator class="native" />
      </id>
      <property name="name" />
      <property name="validFrom" />
      <property name="validTo" />
      <filter name="effectiveDate" condition=":asOfDate BETWEEN validFrom AND validTo" />
   </class>
   <filter-def name="effectiveDate">
      <filter-param name="asOfDate" type="date" />
   </filter-def>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
        // Note: a single parent with three children has been inserted into the db
        //       in another session.
       
        // Enable filter for current date (only one child matches)
        Filter filter = session.enableFilter("effectiveDate");
        filter.setParameter("asOfDate", new Date());

        // Load all children. Only one is returned since filter is active.
        List<Child> children = (List<Child>)session.createQuery("from Child").list();
        System.out.println("children.size()="+children.size());
       
        // Load parent with children. Parent's child collection should contain only
        // one child (but in fact it contains all three!)
        parent = (Parent) session.load(Parent.class, parentId);
        System.out.println("parent.getChildren().size()="+parent.getChildren().size());


Output on STDOUT:
Code:
children.size()=1
parent.getChildren().size()=3


Name and version of the database you are using:
HSQLDB 1.7.3.3 (also tried Firebird 1.5: same problem)

The generated SQL (show_sql=true):
Code:
Hibernate: select child0_.ID as ID1_, child0_.name as name1_, child0_.validFrom as validFrom1_, child0_.validTo as validTo1_ from CHILD child0_ where ? BETWEEN child0_.validFrom AND child0_.validTo
Hibernate: select parent0_.ID as ID0_0_, parent0_.name as name0_0_ from PARENT parent0_ where parent0_.ID=?
Hibernate: select children0_.PARENT_ID as PARENT5_1_, children0_.ID as ID1_, children0_.ID as ID1_0_, children0_.name as name1_0_, children0_.validFrom as validFrom1_0_, children0_.validTo as validTo1_0_ from CHILD children0_ where children0_.PARENT_ID=?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 11, 2005 11:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
If you look at the examples you'll see that you are supposed to apply the filter to the collection mapping if you want the collection filtered.


Top
 Profile  
 
 Post subject: Filter not applied to sub-level elements
PostPosted: Sun Sep 11, 2005 12:51 pm 
Newbie

Joined: Thu Sep 08, 2005 3:49 pm
Posts: 3
Location: Frankfurt, Germany
Umm, thanks a lot ! Of course, now it works.
Somehow looked for the error in all the wrong places...

For the record. The bag must have the filter expression as well. It now reads:
Code:
   <bag name="children" cascade="all" lazy="false">
      <key column="PARENT_ID" />
      <one-to-many class="histest.Child" />
      <filter name="effectiveDate" condition=":asOfDate BETWEEN validFrom AND validTo" />
   </bag>


Joak


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 11, 2005 2:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It might be easier if you define the filter expression in the filter-def. Less typing.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 12:14 pm 
Newbie

Joined: Wed Feb 15, 2006 11:52 am
Posts: 4
Are there plans to add many-to-one filtering in the future?

A common use is to filter on historical data as in the effective date example in the documentation. Filters work well if you are filtering against a collection or a class. The problem is that I also have many-to-one objects that are not supported by filters.

An example, might be an Invoice with a Customer object in a many-to-one relationship. I could apply the filter to the to the Invoice to get a snapshot of the invoice at a point in time, but how can I get the Customer information at the same point?

Is there a recommended solution to this type of problem?


Top
 Profile  
 
 Post subject: We're facing the same issue filters on many-to-one relations
PostPosted: Fri Mar 10, 2006 9:40 am 
Newbie

Joined: Mon Jan 23, 2006 11:08 am
Posts: 5
It would be really nice to have filters on many-to-one relationship.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.