Hello,
We need to run this by the folks in this forum to see if there is something really obvious that we may be missing.
The Problem: Unable to get dynamic filters to filter down a collection. Simplified explanation: say we have a class Foo that contains a collection of objects of type Bar. Both types have a "deleted" property so that we don't actually remove them from our database, we simply mark them as deleted. We are then trying to fetch any non-deleted objects through our queries. We are able to filter down deleted Foo records but not Bar records. The generated SQL query shows that the filter is enabled for Foo, but not Bar.
Question: if you have the patience to take a quick look at the configurations below, can you please let us know if there is something obvious that we are missing? Also, since Spring is involved here, if you feel that this is a Spring issue, please let us know. We'll be happy to take this question to them.
Hibernate version: 3.1.3
Mapping documents:
Simplified for ease of reading:
Code:
<hibernate-mapping>
<class table="foo" name="com.class.Foo">
[...]
<property name="deleted" column="deleted" type="boolean"/>
[...]
<set name="bar" cascade="save-update" lazy="true">
<key column="foo_id" />
<one-to-many class="com.class.Bar" />
<filter name="nonDeletedRecord" condition=":deletedFlag != deleted" />
</set>
[...]
<filter name="nonDeletedRecord" condition=":deletedFlag != deleted" />
</class>
[...]
<class table="bar" name="com.class.Bar">
[...]
<property name="deleted" column="deleted" type="boolean"/>
[...]
<filter name="nonDeletedRecord" condition=":deletedFlag != deleted" />
</class>
</hibernate-mapping>
The filter itself is defined in Spring's LocalSessionFactoryBean using a FilterDefinitionFactoryBean:
Code:
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:</value>
</list>
</property>
<property name="mappingJarLocations">
<list>
<value>WEB-INF/lib/*.jar</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show.sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hbm2ddl.auto">${hbm2ddl.auto}</prop>
</props>
</property>
<property name="entityInterceptor"><ref bean="auditInterceptor"/></property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
<property name="filterDefinitions">
<list>
<bean class="org.springframework.orm.hibernate3.FilterDefinitionFactoryBean">
<property name="filterName" value="nonDeletedRecord"/>
<property name="parameterTypes">
<props>
<prop key="deletedFlag">boolean</prop>
</props>
</property>
</bean>
</list>
</property>
</bean>
Code between sessionFactory.openSession() and session.close():In this case, we are using Spring's HibernateDaoSupport to fetch our data:
Code:
public class FooDAOHibernateImpl extends HibernateDaoSupport {
[...]
public Foo find(String fooId) {
// Enable non-deleted records filter
getSessionFactory().getCurrentSession().enableFilter("nonDeletedRecord").setParameter("deletedFlag", Boolean.TRUE);
List res = getHibernateTemplate().find("from Foo f where f.id=?",fooId);
if(res.size()>0)
return (Foo)res.get(0);
else
return null;
}
[...]
}
Full stack trace of any exception that occurs:
No errors
Name and version of the database you are using:
Postgres 8.2
The generated SQL (show_sql=true):
Hibernate: select foo0_.foo_id as foo1_127_, foo0_.deleted as deleted127_ from foo foo0_ where ? != foo0_.deleted and foo0_.foo_id=?
We thank you in advance for your time and consideration.
Best Regards,
-GS