-->
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.  [ 5 posts ] 
Author Message
 Post subject: @OneToMany Hibernate 3 @Filter annotations need help
PostPosted: Thu Sep 20, 2007 11:06 pm 
Newbie

Joined: Sun Nov 30, 2003 8:51 pm
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.x

Mapping documents:Using annotations

Name and version of the database you are using:Oracle 10g v 2


Hi All,

Thanks in advance to any pointers and info. Google did not do the trick this time around :-(

I have a DB schema design which uses "Soft/virtual Deletes" where every table has a column "del_ind"(number) which indicates(1/0) whether a row is considered "deleted" or not. If the app wants a row to be "deleted", it then sets it to 1. On normal insertion, this column is set to "0".

I am using Hibernate 3 annotations(@FilterDef, @Filter) to filter out Objects that have the "delete_ind" column set to value 1. The Filter is working for the Main Domain Object. But the Filter is not working for the dependent child Object(association).

Details
=======

I have a "Plan" Class that has a OneToMany relationship with Entity "Customer"

Code:
    @Entity
    @Table(name="ASSRT_PLAN")
    @SequenceGenerator(name = "sequence", sequenceName = "ASSRT_PLAN_SEQ", allocationSize = 1)
    @FilterDef(name="limitPlansByDeleteStatus")

    @Filter(
            name="limitPlansByDeleteStatus",
            condition="0 = ASSRT_PLAN_DEL_IND"
    )
    public class PlanImpl implements Plan
    {
        @Column(name="ASSRT_PLAN_DEL_IND")

        private boolean deleted;
     

        @OneToMany(mappedBy="plan", cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity=PlanCustomerImpl.class)
        @Filter(
                name="limitPlanCustomersByDeleteStatus",

                condition="0 = ASSRT_PLAN_CUST_DEL_IND"
        )
        private Set<PlanCustomer> planCustomers = new HashSet<PlanCustomer>();
    .
    .
     
    } // end class PlanImpl
     
    @Entity

    @Table(name="PLAN_CUST")
    @SequenceGenerator(name = "sequence", sequenceName = "PLAN_CUST_SEQ", allocationSize = 1)
    @FilterDef(name="limitPlanCustomersByDeleteStatus")
    @Filter(

            name="limitPlanCustomersByDeleteStatus",
            condition="0 = ASSRT_PLAN_CUST_DEL_IND"
    )
    public class PlanCustomerImpl implements PlanCustomer
    {
        @ManyToOne(cascade = CascadeType.ALL
    , fetch = FetchType.EAGER, targetEntity = PlanImpl.class)
        @JoinColumn(name="ASSRT_PLAN_ID", nullable=false)
        private Plan plan;
     
        @Column(name="ASSRT_PLAN_CUST_DEL_IND")
        private Boolean deleted;

    } // end class PlanCustomerImpl
     
    /* The unit test code  */
                toDeleteCustomer.setDeleted(true);
                planDaoImpl.save(foundPlan);
                Session session = flushSession();
     

                /* Now, lookup the PLAN after enabling the session for "deleted"  */
                Filter filter = session.enableFilter("limitPlansByDeleteStatus");
                session.enableFilter("limitPlanCustomersByDeleteStatus");

                Plan foundAgainPlan = planDaoImpl.getPlanByPlanNumber(TEST_PLAN_NUMBER_1);
     
    assertFalse(foundAgainPlan.findCustomer(DELETE_NUMBER));


I am able to apply the Filter to the Main Entity "PlanImpl". The filter works just fine. But when I apply the Filter to the association of @OneToMany association of Plan to PlanCustomerImpl(as described in THE Hibernate Book) as above, the Filter does not work.

Does anyone know what I need to do to get it right ?

Thank you so much,

BR,
~A


Top
 Profile  
 
 Post subject: Re: @OneToMany Hibernate 3 @Filter annotations need help
PostPosted: Fri Aug 07, 2009 6:09 pm 
Newbie

Joined: Thu Oct 02, 2008 1:42 pm
Posts: 1
Hello,

Have you resolved this issue or found any work around ? I am facing with the same issue of @Filter being ignored by @OneToMany Collection.

thx,
Ming


Top
 Profile  
 
 Post subject: Re: @OneToMany Hibernate 3 @Filter annotations need help
PostPosted: Tue Nov 03, 2009 5:41 pm 
Newbie

Joined: Tue Nov 03, 2009 5:39 pm
Posts: 2
Any update or comments on this, anyone? Is this how hibernate filters are supposed to work?


Top
 Profile  
 
 Post subject: Re: @OneToMany Hibernate 3 @Filter annotations need help
PostPosted: Fri Nov 20, 2009 2:07 pm 
Newbie

Joined: Mon Apr 28, 2008 4:32 pm
Posts: 2
I have found the same thing. Applying a filter to a field on an entity works when querying that entity. Applying a filter on a @OneToMany collection to prevent navigation does not appear to work.

I have tried annotations and mapping files and neither have worked.


Top
 Profile  
 
 Post subject: Re: @OneToMany Hibernate 3 @Filter annotations need help
PostPosted: Sun Jul 18, 2010 9:42 am 
Newbie

Joined: Thu Jul 15, 2010 6:44 am
Posts: 13
Finally I was able to crack the filters working, either it's by design or by fault not sure, but this how filters will work even with your collections.

You need to make sure your collections are lazily loaded, if you want the enabled filters of the current session to be applied on them.

The filters will only work with collections if they are lazily loaded, eagerly loaded collections don't respect the enabled filters of the session.

Enjoy, using filters they are really very powerful feature if you have versioned data to work with.

It worked with Hibernate-3.5.3-final version, below is the hbm mapping I used, hope it helps.

Code:
<class name="Employee" table="emp" entity-name="VersionedEmp">
      <id name="id">
      </id>
      <property name="name" column="name" not-null="false" />
      <property name="description" column="description" not-null="false" />
      <property name="startDate" column="start_date" type="timestamp"
         not-null="true" />
      <property name="endDate" column="end_date" type="timestamp"
         not-null="false" />
      <property name="version" column="version" not-null="true" />

      <many-to-one name="department" class="Department" column="dept_id"
         not-null="false" lazy="false" />

      <bag name="addresses" lazy="true">
         <key column="emp_id" ></key>
         <one-to-many class="Adress" entity-name="VersionedAddress"/>
         <filter name="byVersion" />
      </bag>   
      <filter name="byVersion"/>
   </class>

   <class name="Address" table="address" entity-name="VersionedAddress">
      <id name="id">
      </id>
      <property name="line1" column="line1"/>
      <property name="line2" column="line2"/>
      <property name="startDate" column="start_date" type="timestamp"
         not-null="true" />
      <property name="endDate" column="end_date" type="timestamp"
         not-null="false" />
      <property name="version" column="version" not-null="true" />
      <filter name="byVersion"/>
</class>

<filter-def name="byVersion">
   <filter-param name="asAtDate" type="timestamp"/>
   (start_date &gt;= :asAtDate and (end_date is null or end_date &lt;= :asAtDate))
</filter-def>


_________________
Vikram.
Http://enterprisejava4u.blogspot.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.