-->
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: Defining and attaching data filters dynamically
PostPosted: Wed Jan 18, 2006 12:35 pm 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
I am looking for a way to add a data filter (from chapter 17 of the Hibernate 3 documentation: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#filters), but I would like to define it and attach to persisted classes at run-time, without changing the mapping files. I need this because the description of what needs to be filtered out comes from another system in the form of an object representing a parameterized predicate - it does not become available until run-time. Is there a way to do this? If the answer is yes, where can I find more information about it?
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 7:36 am 
Newbie

Joined: Mon Jan 23, 2006 11:04 am
Posts: 7
see Session.createFilter(...), it allows dynamic 'where' clause building or you could simply add Criteria on the fly to your dao:

Code:
    protected List<T> findByCriteria(Criterion... p_criterion)
    {   //varargs ...nice :-)
        Criteria crit = getCurrentSession().createCriteria(getPersistentClass());
        for (Criterion c : p_criterion)
        {
            crit.add(c);
        }
        return crit.list();
    }


Hope this helps, pls don't forget to rate !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 7:36 am 
Newbie

Joined: Mon Jan 23, 2006 11:04 am
Posts: 7
see Session.createFilter(...), it allows dynamic 'where' clause building or you could simply add Criteria on the fly to your dao:

Code:
    protected List<T> findByCriteria(Criterion... p_criterion)
    {   //varargs ...nice :-)
        Criteria crit = getCurrentSession().createCriteria(getPersistentClass());
        for (Criterion c : p_criterion)
        {
            crit.add(c);
        }
        return crit.list();
    }


Hope this helps, pls don't forget to rate !


Top
 Profile  
 
 Post subject: I figured it out...
PostPosted: Tue Jan 24, 2006 1:43 pm 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
Adding criteria programmatically was not the thing I was trying to do.

I figured out a way to do what I needed - in order to add filters dynamically, filter definitions need to be added to the Configuration object before the SessionFactory is built from it. Once the definitions are added, one could go through persistent classes and attach filters to their mappings and/or the mappings of their collections. Finally, you call Configuration.buildSessionFactory, and the sessions the session factory creates will have the filters you defined programmatically.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 7:41 am 
Newbie

Joined: Mon Jan 23, 2006 11:04 am
Posts: 7
cool, I was wondering and this may not be a scenario you required but did you have to apply filters to all filterable POJO implmentations or to a super class only. e.g. an inactive/active flag applied to a Persistent super class, rather than each concrete POJO implementation?

Also I'm using Spring to manage my SessionFactory, I found it easier to define the filters at package level (package-info.java via annotations)... (couldn't get them built into the hbm sessionFactory from the spring-context.xml but this should be possible too), then 'enable'/'disable' them against the session dynamically at runtime BUT I'm still left with having to map individual filters to each concrete POJO implementation which is what I was trying to avoid for 'global' filters for access-level, logical-delete etc.


Top
 Profile  
 
 Post subject: Applying dynamically created filters
PostPosted: Mon Jan 30, 2006 11:27 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
I apply filters to all mapped classes deriving from the ones on which the filters are actually defined. To avoid doing this manually, I wrapped the SessionFactory and added a call to my own enableFilters method in createSession(...) methods of the factory. This was a very convenient trick - I needed a separate method to "decorate" the configuration with filters before making a SessionFactory, so I created a method makeSessionFactory returning a SessionFactory. That method adds the filters to the Configuration passed into it, creates a real SessionFactory from the "decorated" configuration, and add a wrapper around the resulting SessionFactory to enable my filters (of course the wrapper gets a list of the filters it needs to enable, and a map of arguments that it needs to set on each one, so the users of the session have no idea the filter is there).


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.