SetParameter and setParameterList in hibernate pre-compiled SQL statements, and hibernate pre-compiled

Source: Internet
Author: User

SetParameter and setParameterList in hibernate pre-compiled SQL statements, and hibernate pre-compiled

Use pre-compiled SQL statements and placeholder parameters (which is in jdbc ?), This avoids the complexity of concatenating SQL statements with strings. Let's take a simple look at the advantages of using precompiled SQL statements. Use String SQL = "select * from Student where name =" + name; if the name value is 1 or "aty" or "aty 'aty", the following error SQL statement is generated:

--ORA-01722 invalid numberselect * from student where name=1;--ORA-00904 invalid identifierselect * from student where name=aty;--ORA-01756: quoted string not properly terminatedselect * from student where name=aty'aty;

When constructing an SQL statement, if a string is concatenated, you must consider the data type and whether to add single quotes or other details. If you do not pay attention to it, the SQL statement will be incorrect. When many strings are concatenated, the code is almost unreadable, and it is very difficult to locate the problem. This is the advantage of precompiled SQL in code readability and simplicity. There is also the performance advantage, you can refer to my another blog: HQL or SQL use? Benefits: reduce SQL parsing time, reduce memory overhead, and prevent SQL injection.

 

JDBC provides PreparedStatement. setXXX () to replace the placeholder parameter. hibernate corresponds to setParameter and setParameterList.

The difference between setParameter and setParameterList is that in is used.

Object[] params = new Integer[]{1, 2};String hqlF = "from Student where id in (?,?)";Query query = session.createQuery(hqlF);for (int i = 0; i < params.length; i++){    query.setParameter(i, params[i]);}//String hqlS = "from Student where id in :valueList";String hqlS = "from Student where id in (:valueList)";Query queryS = session.createQuery(hqlS);     queryS.setParameterList("valueList", params);

Obviously, using setParameterList is simpler. Here we also praise the hibernate API design, which provides both the conventional and tedious setParameter method and the simple and easy-to-use setParameterList method. This consistency provides more options for those who are familiar with and not familiar with hibernate.


What is the use of the setParameter () method of hibernate?

Set your query parameters.

For example, "from Customer o where o. name =? 1"

SetParameter (1, "java ");
==================================

Yes, but it is not recommended because you have to pay attention to SQL injection and other issues.

QuerysetParameter in Hibernate cannot add Parameters

Select * Topic as model where model. boardId =? Limit is not a problem of value assignment. Your hql statement is incorrect. There is no from keyword here. Which table do you want to check for ing?

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.