enabling grails hibernate filters

空扰寡人 提交于 2019-12-01 00:48:34

After a little digging around I have come up with a workaround for the above problem

I basically extend the GrailsOpenSessionInViewFilter class

public class OpenSessionInViewFilterExt extends GrailsOpenSessionInViewFilter{
@Override
protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
    Session session = super.getSession(sessionFactory);
    for (Object filterName : DefaultHibernateFiltersHolder.getDefaultFilters()) {
        session.enableFilter((String) filterName);
    }
    return session;
}

}

I also have a entry in the web.xml

    <filter>
        <filter-name>OpenSessionInViewFilterExt</filter-name>
        <filter-class>com.nthdimenzion.hibernate.ext.OpenSessionInViewFilterExt</filter-class>
</filter>


<filter-mapping>
    <filter-name>OpenSessionInViewFilterExt</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Now each time a new session is created the default filters are enabled for it. I think this should work, however it'll be better if some change can be made in the zkgrails plugin or the hibernate filters plugin so that the two can co-exist in a single application :)

Thanks

In my Grails 2.5.X app I enable the hibernate filters in a web filter, i.e. I have this code in grails-app/conf/Filters.groovy

    class Filters {
        publishedContentFilter(uri: '/**'){
            before = {
                // enable the 'published filter in the Condition domain class
                Condition.enableHibernateFilter('published')
            }
        }
    }

In the integration tests in the plugin itself, the filters are enabled in the setup method of the test class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!