Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Filter Programmatically

How to programmatically achieve the same result (as the following annotation would achieve)? Or injecting the value 'SMITH' in a spring configuration file?

@Filter(name="smithFilter", condition="LAST_NAME = 'SMITH'")
public String getLastName()
{
    return this.lastName;
}
like image 922
SoftwareDeveloper Avatar asked Mar 10 '11 01:03

SoftwareDeveloper


1 Answers

Just define a FilterDef with the parameters the filter condition will receive:

@FilterDef(name = "smithFilter", parameters = {@ParamDef(name = "lastName", type = "string")})
@Filter(name="smithFilter", condition="LAST_NAME = :lastName")

As you also used the word "programmatically", you may also want to know that you can set the parameter based on other inputs, like this:

session.enableFilter("smithFilter").setParameter("lastName", "SMITH");

More details in Hibernate documentation:

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#objectstate-filters

like image 144
jpkrohling Avatar answered Sep 22 '22 16:09

jpkrohling