My question is this : Are Hibernate filters applied and translated or are they only appended after the HQL has already been translated into sql?
I would like to use Hibernate filters as a way to enforce some security parameters by being a member of a group on certain objects. I would like to have a many to many relationship on each of the secured objects and create a filter that will limit the objects that are returned by forcing the objects to be in a certain group.
I created a filter that is a list of groups like this:
Code:
<filter-def name="SecurityFilter">
<filter-param name="OwnerGroupID" type="java.util.ArrayList"/>
</filter-def>
I then created a many to many relationship on a test object and setup the filter:
Code:
<set name="owningGroups" table="OBJECT_GROUP_JOIN" cascade="all" lazy="true" >
<key column="OBJECT_ID"/>
<many-to-many column="GROUP_ID" class="com.entity.Group"/>
</set>
<filter name="SecurityFilter" condition="owningGroups.id in (:OwnerGroupID)"/>
In the same file I have a named query that I would like to use:
Code:
<query name="com.entity.SecureObject.findAllInGroup">
select distinct so from SecureObject so
</query>
I then enable the filter with a set of group objects :
Code:
Group groupone = new Group( "one" );
Group grouptwo = new Group( "two" );
List<Group> groupIDs = new ArrayList<Group>();
groupIDs.add(groupOne);
groupIDs.add(groupTwo);
session.enableFilter("SecurityFilter").setParameter("OwnerGroupID", groupIDs);
// Find all the groups using the filter
List<SecureObject> list = this.getSession().getNamedQuery(fullQueryName("findAll")).list();
I was hoping that Hibernate would translate the so.owningGroups.id in into a join to the groups table to get the correct owned objects. I believe that Hibernate is simply appending the filter string to the query and not translating the filter into it's correct sql. The sql that is generated is :
Code:
select secureobje0_.ID as ID, secureobje0_.NAME as NAME0_ from domain.SECURE_OBJECT secureobje0_ where owningGroups.id in (?, ?)
If I change the named query to be :
Code:
select distinct so from SecureObject so where so.owningGroups.id in ( :groups )
Hibernate does exactly what I would like it to do.
Is there something that I am not doing correctly?
Thanks
Ryan
Hibernate version:
3.0.1
Name and version of the database you are using:
Oracle 8.1.7