What is the difference between writing a hql statement and not writing a hql statement?
Write HQL statement : Write the HQL statement, all the query and projection design is done using the HQL statement.
Do not write HQL statement : No query statements, all the query and projection design using object-oriented format to complete.
The timing of the choice:
Do not write hql statements, sometimes more convenient, without regard to complex queries, can be used , This method of some Hibernate methods encapsulated, inflexible.
Write hql statements, complex business logic, recommended use, flexible and convenient.
The following comparison between the two methods to highlight the hibernate does not write hql a simple use.
Write hql statement:
With a lot of flexibility, can provide powerful query function.
For example,String hql = "from User", querying all database fields corresponding to the user entity class.
Session session = this. getcurrentsession (); String hql = "from User"; Query q = session.createquery (HQL); list<user> users = q. List (); return users; |
such as:String hql= "Update user user set user.age=22 where user.age=12"; Update User the data record corresponding to the entity class.
Session session = this. getcurrentsession (); String hql = "Update user user set user.age=22 where user.age=12"; Query q = session.createquery (HQL); Q.uniqueresult (); return null; |
such as:String hql="Delete from user user where user.age=19"; Delete the data record corresponding to the User entity class.
Session session = this. getcurrentsession (); String hql = "Delete from user user where user.age=?"; Query q = session.createquery (HQL); Q.setparameterlist ("Age", age); Q.executeupdate (); return null; |
do not write hql statement:
To query the database field data corresponding to the User entity class, you can use the following method:
Get all the data records in the user's corresponding data Public list<user> getdeplist () throws exception{ Detachedcriteria DC = Detachedcriteria.forclass (User.class); Return Gethibernatetemplate (). Find (DC); } |
Detachedcriteria is an offline query.
to construct a corresponding conditional query, you can look like this:
Public list<user> getdeplist () throws exception{ detachedcriteria DC = detachedcriteria.forclass (user.class); if (user != null) { if (User .getname ()!=null &&!user .getname (). Equals ("")) { dc.add (Restrictions.ilike ("name", User.getName (), Matchmode.anywhere)); } if (User.gettele ()!=null &&!user.gettele (). Equals ("")) { dc.add (Restrictions.ilike ("Tele", User.gettele (), Matchmode.anywhere)); } } return gethibernatetemplate (). Find (DC); } |
Restrictions for the projection query, which encapsulates a variety of query conditions, such as above,ilike () represents a fuzzy query, the inside of the three parameters, respectively, the "database field", "corresponding field value", "Fuzzy query matching mode."
In addition the template encapsulates a lot of methods about query conditions:
To add a record, you can refer to the following methods:
Gethibernatetemplate (). Save (user); Save an entity class directly with a template, before which the data should be encapsulated in the user entity class. |
In addition, the template also encapsulates some other save - like methods:
To modify a record in a table, you can refer to the following methods:
Gethibernatetemplate (). Update (user);
The same template can be updated directly.
In addition, the template also encapsulates some of the similar methods related to updates:
To delete a record in a table, you can refer to the following method:
Gethibernatetemplate (). Delete (user);
You can also do this directly using a template.
In addition, the template also encapsulates some of the similar methods that are related to deleting:
Because of limited knowledge, writing relatively thin, hope to guide enlighten.
What is the difference between hibernate writing HQL statements and not writing HQL statements?