"SSH" Hibernate: Common HQL statements

Source: Internet
Author: User


Hibernate Query Language abbreviated to HQL, is the longest in the real development of hibernate package mode.

HQL provides a query method that is closer to the traditional SQL statement:

<span style= "FONT-FAMILY:VERDANA;FONT-SIZE:14PX;" >[select/update/delete ...] [From ...] [Where ...] [GroupBy ...] [Having ...] [Order BY ...] </span>

Entity query:

<span style= "FONT-FAMILY:VERDANA;FONT-SIZE:14PX;" >string hql = "from TUser"; Query Query =session.createquery (HQL); List uerlist = query.list ();</span>

where select * from TUser in the "from TUser" ==sql. is to poll all entities, and if Tuser has a sub-class table below it, all the data for the subclass table will be found.

How to query a specified entity:

<span style= "FONT-FAMILY:VERDANA;FONT-SIZE:14PX;" >string hql = "from TUser (AS) userwhere user.name= ' * * * *" </span>


Property query:

Query the Name property collection for all entities:

<span style= "FONT-FAMILY:VERDANA;FONT-SIZE:14PX;" >list list =session.createquery ("Selectuser.name,user.age from TUser as user"). List ();</span>


Query the Name property collection for age =15:

<span style= "FONT-FAMILY:VERDANA;FONT-SIZE:14PX;" >list list =session.createquery ("Selectuser.name,user.age from TUser as user where Age = '"). List ();</span>

Update:

<span style= "FONT-FAMILY:VERDANA;FONT-SIZE:14PX;" >transaction TX =session.begintransaction ();//reference transaction string hql = "Update TUser set age =18 Whereid = 1"; Query Query =session.createquery (HQL);. Query.executeupdate ();.tx.commit</span>

Delete:

<span style= "FONT-FAMILY:VERDANA;FONT-SIZE:14PX;" >     String hql= "Delete TUser whereage=18";</span>


In the hql above, the parameters are directly represented in the HQL, which raises a problem similar to SQL injection events in the original SQL. For example, when you log in to enter a user name: ' liming ' or ' x ' = ' x ', which appears as follows when passed into the HQL statement:

<span style= "FONT-FAMILY:VERDANA;FONT-SIZE:14PX;" > from      TUser user where user.name= ' liming ' or ' x ' = ' x ' and user.pwd= ' * * * * ......</span>


At this point you can see that the password to determine the logic is true, as long as the name value is correct, there is no password will be successful login. So like the JDBC SQL, hibernate passes the sequential placeholder "? "To circumvent this problem. use "?" for the parameters passed in at query time. instead


HQL and SQL are very similar, they are very uncomfortable to use at first, and now look back, they are so similar.


"SSH" Hibernate: Common HQL statements

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.