Hibernate Reading Notes ----- Data Filtering

Source: Internet
Author: User

Hibernate3 provides an innovative way to process data with "explicit" rules, that is, using Hibernate filter. Hibernate filter is a global filter with a name and parameters. For a specific Hibernate session, you can choose whether to enable or disable a filter.
Once the data filter is enabled, whether it is data query or data loading, the filter will automatically act on all data, and only records that meet the filter conditions will be selected.
A filter condition is equivalent to defining a constraint clause that is very similar to a class and a set of "where" attributes. However, a filter condition can contain parameters. When running, the application can decide whether to enable the given filter and the parameter values used. The usage of filters is similar to the database view, except that the parameters used are determined in the application.
The filter consists of the following steps:
1. Define a filter.
To use a filter, you must first define it in the corresponding ing node. To define a filter, use the <filter-def/> node located in the [Html]
<Filter-def name = "myFilter">
 
<Filter-param name = "myFilterParam" type = "string"/>
 
</Filter-def>
2. Use a filter.
After being defined, you can use this filter in a class. Use the <filter.../> element to apply the specified filter to the specified persistence class.
[Html]
<Class name = "myClass"...>
 
...
 
<Filter name = "myFilter" condition = ": myFilterParam = MY_FILTERED_COLUMN"/>
 
</Class>
The Condition attribute value is an SQL-style where clause. Therefore, the filter conditions specified by the condition attribute should be filtered Based on the indication and column name.
You can also use it in a collection:
[Html] view plaincopyprint?
<Set...>
 
<Filter name = "myFilter" condition = ": myFilterParam = MY_FILTERED_COLUMN"/>
 
</Set>
You can use a filter in multiple classes or sets. You can also use multiple filters in a class or set.
3. Enable the filter through Session in the program.
The methods used in the Session object include: enableFilter (String filterName), getEnabledFilter (String filterName), and disableFilter (String filterName ). the filter is disabled by default in the Session, and must pass the Session. enabledFilter () method is explicitly enabled. This method returns the instance of the enabled Filter. The filter defined above is used as an example:
Session. enableFilter ("myFilter"). setParameter ("myFilterParam", "some-value ");
The following is a complete instance.
Filter configuration file
[Html]
<Hibernate-mapping package = "com. hibernate. domain">
<! -- Map the Person persistence class -->
<Class name = "Person" table = "person">
<! -- Ing identity Attributes -->
<Id name = "id" column = "person_id">
<! -- Specify the primary key generation policy for identity -->
<Generator class = "identity"/>
</Id>
<! -- Map common attributes -->
<Property name = "name" type = "string"/>
<Property name = "age" type = "int"/>

<! -- Use data filtering -->
<Filter name = "myFilter" condition = ": filterparam = age"/>
</Class>

<! -- Define Filter -->
<Filter-def name = "myFilter">
<Filter-param name = "filterparam" type = "int"/>
</Filter-def>
</Hibernate-mapping>

Example program:
[Java]
Public class PersonManager {
Public static void main (String [] args ){
Session session = HibernateUtil. getSession ();
Transaction tx = session. beginTransaction ();

// Start the myFilter filter and set parameters

Session. enableFilter ("myFilter"). setParameter ("filterparam", 30 );
// Query the person entity without any filtering Conditions
List list = session. createQuery ("from Person as p"). list ();
For (Iterator iterator = list. iterator (); iterator. hasNext ();){
Person person = (Person) iterator. next ();
System. out. println (person. getName ());
}
}
}
Generally, if a filtering condition is frequently used, we can set it as a filter. If it is temporary data filtering, it is better to use regular queries.
 

Author: chenssy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.