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