Parameter bindings in the Hibernate-orm:07.hibernate

Source: Internet
Author: User

Tag: is the obj parameter binding print parameter int span class Erb

------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------

This blog will explain the parameter binding in Hibernate , which is equivalent to the condition behind the Where in the SQL statement

One, explain the summary :

1. binding parameters by subscript

2. binding parameters by custom parameter names (more for multi-table operations)

3. bind parameters by passing in custom objects (more for single-table operations)

4. bind parameters in a way similar to smart tags (more for multi-table operations with conditions)

Two, bind the parameter by the way of subscript
@Test/*specify parameters by subscript*/     Public voidT01parameterbyunderid () {/*simulating foreground input parameters*/Teacher Teacher=NewTeacher ("A", 400.00); /*HQL Statements*/String hql= "from Teacher where address=?" and Sal>? "; Query Query=session.createquery (HQL); /*Assign Value*/Query.setparameter (0, Teacher.getaddress ()); Query.setparameter (1, Teacher.getsal ()); /*Enquiry*/List List=query.list (); /*Traverse*/         for(Object t:list) {System.out.println (t); }    }

Third, the parameter is bound by the way of the custom parameter name (more for multi-table operation)
@Test/*specifying parameters by customizing parameter names*/     Public voidT02parameterbymyselfkey () {/*simulating foreground input parameters*/Teacher Teacher=NewTeacher ("A", 400.00); /*HQL Statements*/String hql= "from Teacher where Address=:teaadd and Sal>:teasal"; Query Query=session.createquery (HQL); /*Assign Value*/Query.setparameter ("Teaadd", Teacher.getaddress ()); Query.setparameter ("Teasal", Teacher.getsal ()); /*Enquiry*/List List=query.list (); /*Traverse*/         for(Object t:list) {System.out.println (t); }    }

Four, binding parameters by passing in custom objects (more for single-table operations)
@Test/*specifying parameters by customizing the object*/    /*suitable for single-table operation*/     Public voidT03parameterbymyselfobject () {/*simulating foreground input parameters*/Teacher Teacher=NewTeacher ("A", 400.00); /*HQL Statements*/String hql= "from Teacher where address=:address and Sal>:sal"; Query Query=session.createquery (HQL); /*Assign Value*/query.setproperties (teacher); /*Enquiry*/List List=query.list (); /*Traverse*/         for(Object t:list) {System.out.println (t); }    }

Five. bind parameters in a way similar to smart tags (more for multi-table operations with conditions)
@Test/*specifying dynamic parameters by customizing parameter names*/    /*for multi-table operations*/     Public voidT04parameterbydongtai () {/*simulating foreground input parameters*/Teacher Teacher=NewTeacher ("A", 400.00); /*HQL Statements*/StringBuffer hql=NewStringBuffer ("From Teacher where 1=1"); if(Teacher.getaddress ()! =NULL) {hql.append ("And Address=:teaadd"); }        if(Teacher.getsal () >0) {hql.append ("And Sal>:teasal"); } Query Query=Session.createquery (hql.tostring ()); /*Assign Value*/Query.setparameter ("Teaadd", Teacher.getaddress ()); Query.setparameter ("Teasal", Teacher.getsal ()); /*Enquiry*/List List=query.list (); /*Traverse*/         for(Object t:list) {System.out.println (t); }    }

Parameter bindings in the Hibernate-orm:07.hibernate

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.