Hibernage-parameter binding-Pre-Compilation

Source: Internet
Author: User

Hibernate provides rich support for dynamic query parameter binding. What is dynamic query parameter binding? In fact, if we are familiar with traditional JDBC programming, it is not difficult to understand the dynamic binding of query parameters. The following code binds parameters of traditional JDBC:
PrepareStatement pre = connection. prepare ("select * from User where user. name = ?");
Pre. setString (1, "zhaoxin ");
ResultSet rs1_pre.exe cuteQuery ();
In Hibernate, the query parameter binding function similar to this type is also provided. In Hibernate, this function also provides many features that are richer than traditional JDBC operations, there are four parameter binding methods in Hibernate. We will introduce them as follows:
A. Bind by parameter name:
To define a name parameter in an HQL statement, start with ":". The format is as follows:
Query query = session. createQuery ("from User user where user. name =: customername and User: customerage =: age ");
Query. setString ("mermername", name );
Query. setInteger ("mermerage", age );
In the above Code, mername and: customerage are used to define the customername and customerage respectively, and then the setXXX () method of the Query interface is used to set the name parameter value. The setXXX () method contains two parameters, they are the name of the named parameter and the actual value of the named parameter.
B. According to the parameter location:
Use "?" In the HQL query statement "?" Parameter location, in the following format:
Query query = session. createQuery ("from User user where user. name =? And user. age =? ");
Query. setString (0, name );
Query. setInteger (1, age );
Similarly, the setXXX () method is used to set the binding parameter, except that the first parameter of the setXXX () method represents the position number of the bonding parameter in the HQL Statement (starting from 0 ), the second parameter still represents the actual value of the parameter.
Note: In actual development, naming parameters by name are recommended, because this not only provides good program readability, but also improves the maintainability of the program, because when the location of the query parameter changes, you do not need to adjust the program code in the naming method.
C. setParameter () method:
In the HQL query of Hibernate, you can use the setParameter () method to specify any type of parameters, as shown in the following code:
String hql = "from User user where user. name =: customername ";
Query query = session. createQuery (hql );
Query. setParameter ("mermername", name, Hibernate. STRING );
As shown in the code above, the setParameter () method contains three parameters: the named parameter name, the actual value of the named parameter, and the ing type of the named parameter. The setParameter () method of some parameter types can guess the corresponding ing type based on the Java type of the parameter value. Therefore, you do not need to display the ing type, as shown in the preceding example, you can directly write it like this:
Query. setParameter ("mermername", name); however, for some types, you must specify the ing type, such as java. util. date type, because it corresponds to multiple Hibernate ing types, such as Hibernate. DATA or Hibernate. TIMESTAMP.
D. setProperties () method :( setEntity ())
In Hibernate, you can use the setProperties () method to bind the named parameter with the property value of an object. The following program code:
Customer customer = new Customer ();
Customer. setName ("pansl ");
Customer. setAge (80 );
Query query = session. createQuery ("from Customer c where c. name =: name and c. age =: age ");
Query. setProperties (customer );
The setProperties () method automatically matches the property value of the instance of the customer object to the name parameter, but requires that the name of the name parameter must be the same as the property of the object.
There is also a special setEntity () method, which associates the named parameter with a persistent object, as shown in the following code:
Customer customer = (Customer) session. load (Customer. class, "1 ");
Query query = session. createQuery ("from Order order where Order. customer =: customer ");
Query. setEntity ("customer", customer );
List list = query. list ();
The above code generates an SQL statement similar to the following:
Select * from order where customer_ID = '1 ';
E. advantages of using bound parameters:
Why do we need to bind a named parameter? The existence of any thing has its own value. Specifically, binding parameters has the following two main advantages for HQL queries:
① You can use the database to implement performance optimization. For Hibernate, PrepareStatement is used at the underlying layer to complete the query. Therefore, for SQL statements with the same syntax and parameters, you can make full use of the pre-compiled SQL statement cache to improve query efficiency.
② Prevent SQL Injection security vulnerabilities:
SQL Injection is an attack method specially designed for SQL statement Assembly. For example, for common user logon, you can enter the user name and password on the logon interface, at this time, the login verification program may generate the following HQL statement:
"From User user where user. name = '" + name + "' and user. password = '" + password + "'"
This HQL statement is logically correct, and the logon verification function is generally completed correctly, however, if you enter "zhaoxin or 'X' = 'x" in the user name during logon, the following HQL statement is generated if the string of a simple HQL statement is assembled:
"From User user where user. name = 'zhaoxin' or 'X' = 'X' and user. password = 'admin '";
Obviously, the where clause of this HQL statement will always be true, making the role of the user's password meaningless. This is the basic principle of SQL Injection attacks.
Using the parameter binding method, you can properly handle this problem. When using the parameter binding method, you will get the following HQL statement:
From User user where user. name = ''zhaoxin' or ''x = ''x' and user. password = 'admin'; therefore, using the binding parameter will resolve the single quotation marks entered in the user name to a string (if you want to include single quotation marks in the string, you should use repeated single quotation marks ), therefore, parameter binding can effectively prevent SQL Injection security vulnerabilities.

Author: ERDP Technical Architecture"

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.