Getting Started with Hibernate (9): Data filtering

Source: Internet
Author: User
Hibernate Data filtering

1, data filtering can be understood as a special way of data query, this is a whole data filtering method, once the data filter is enabled, the filter will automatically act on the specified scope, only the data to meet the filter record can be selected, from this point of view, Hibernate provides a data filtering mechanism, Can be regarded as an auxiliary means of conditional query; 2. Hibernate provides the following annotations to support filter @FilterDef: Configure filters, @Filter: Apply filters;
3. Use examples The following example configures 2 filters, a global filter, and a local filter User.java
/* 2 Filter Filters Effectiveage: Global filter For filtering the Age property, filter effectivedate: Local filter For filtering the CreateDate attribute of the associated entity comment *// Configures the parameter type of the filter associated with user @FilterDefs ({@FilterDef (name = "Effectivedate",//filter name "E Ffectivedate "parameter list parameters = {@ParamDef (name =" Datelimit ", type =" date ")}),//The filter contains a parameter" Datelimit ", type is date @FilterDef (name =" Effectiveage ", parameters = {@ParamDef (name =" Agelimit ", type =" int ")}) @Entity @Table (name=" users ") @Filter (name=" effectiveage ", condition =" Age >=: Agelimit ")//Global filter Effectivea
    
    GE public class User {@Id @Column (name= "user_id") private int Id;
    
    @Column (name= "user_name") private String name;

    @Column (name= "User_age") private String age; @OneToMany (targetentity = Comment.class,cascade=cascadetype.all,mappedby = "user") @Filter (name= "Effectivedate", Condition = "CreateDate >=:d atelimit")//local filter effectivedate private set< comment> comments = new hashset<> ();
Test.java
public class Test {public
    static void Main (string[] args) throws exception{
        Session session = Hibernateutil.curr Entsession ();
        Transaction tran = Session.begintransaction ();

        Start two filters and configure the parameter
        session.enablefilter ("Effectivedate")
                . Setparameter ("Datelimit", New SimpleDateFormat ("Yyyy-mm-ss"). Parse ("2016-01-01"));
        Session.enablefilter ("Effectiveage")
                . Setparameter ("Agelimit");

        List List = Session.createquery ("Select u from Users u")
                . List ();

        Tran.commit ();
        Hibernateutil.closesession ();
    }
}

Related Article

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.