Hibernate Filter Programmatically

How to achieve the same result programmatically (how will the following annotation be achieved)? Or enter the value "SMITH" in the spring configuration file?

@Filter(name="smithFilter", condition="LAST_NAME = 'SMITH'")
public String getLastName()
{
    return this.lastName;
}
+5
source share
1 answer

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

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

Since you also used the word "programmatically", you may also know that you can set the parameter based on other inputs, for example:

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

More details in the Hibernate documentation:

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

+4
source

All Articles