Six ways to implement Hibernate queries, and IDE recommendations

Source: Internet
Author: User
Tags cdata


These days have been a good mess, perhaps because of the test, a little slack in my mind. Maybe it's something that's been happening to me lately.
it. I feel like I don't know myself anymore. I am grateful to some people for their teachings. Because a lot of words are very helpful to me and
Inspire, but also let me in addition to do technology, play programming, understand a lot of truth, perhaps this is a person mature process it. I really hope so.
Hope I can forget the previous ignorance, forget the previous frivolous, do the technology.

Their "truth" can sometimes be wrong, and their ideas are sometimes "naïve". I really want to be quiet. I saw it after supper.

A senior in the Baidu Internship wrote a journal, think write is very good, understand a master of a definition, or is a realm, a

Advanced things.

I would like to start from now, so that they can really enjoy the joy of programming, no vanity, no competition, no distractions.
Only happiness, only enrichment, no regrets. Of course, I do what I say, the diary is about a point to the face, from the face to the points of philosophy.
I think it is necessary to write a summary of hibernate, from the point to the surface, about Hibernate query 6 ways. is hql query
, object query criteria method, dynamic query Detachedcriteria, example query, SQL query, named query
.

If you simply use Hibernate to query the database only need to understand one of them can accomplish the general function you want to implement, but
From one point, let's grasp the 6 method, then provide more choices. Each method has its own applicable conditions and prerequisites.

HQL Query

HQL is Hibernate's own set of query languages, different from SQL syntax, with the advantages of cross-database. Example code:

static void query (String name) {
Session S=null;
try{
S=hibernateutil.getsession ();

From after is an object, not a table name
String hql= "from admin as admin where admin.aname=:name";//use named parameters, recommended, easy to read.
Query query=s.createquery (HQL);
Query.setstring ("name", name);

List<admin> list=query.list ();

for (Admin admin:list) {
System.out.println (Admin.getaname ());
}
}finally{
if (s!=null)
S.close ();
}
}

Application: Common method, more traditional, similar to JDBC. Cons: New query Language, limited applicability, applies only to hibernate framework.

Object Query criteria method :

static void Cri (String name,string password) {
Session S=null;
try{
S=hibernateutil.getsession ();

Criteria C=s.createcriteria (Admin.class);
C.add (Restrictions.eq ("Aname", name));//eq is equal to, GT is greater than, LT is less than, or is or
C.add (Restrictions.eq ("Apassword", password));

List<admin> list=c.list ();
for (Admin admin:list) {
System.out.println (Admin.getaname ());
}
}finally{
if (s!=null)
S.close ();
}
}

Application: Object-oriented operation, innovation of the previous database operation mode, easy to read. Disadvantage: The application surface is more limited than HQL.

Dynamic Separation Query Detachedcriteria

Static List DC (Detachedcriteria DC) {

Session s = hibernateutil.getsession ();
Criteria C = Dc.getexecutablecriteria (s);
List rs = c.list ();
S.close ();
Return RS;
}

Detachedcriteria DC = Detachedcriteria.forclass (User.class);
int id = 1;
if (id! = 0)
Dc.add (RESTRICTIONS.EQ ("id", id));
Date age = new Date ();
if (age! = null)
Dc.add (Restrictions.le ("Birthday", age));
List users = DC (DC);
SYSTEM.OUT.PRINTLN ("Offline query returns results:" + users);

Application: Object-oriented operations, separation of business and the underlying, do not require the field attribute ingestion to the DAO implementation layer. Disadvantage: The application surface is more limited than HQL.

Example Query

Static List example (user user) {
Session s = hibernateutil.getsession ();
list<user> users = S.createcriteria (User.class). Add (
Example.create (user)). List ();
List<user>
Users2=s.createcriteria (User.class). Add (Example.create (User)). IgnoreCase ())
. Createcriteria ("Child"). Add ((example.create (user))). List ();
return users;
}

Where applicable: Object-oriented operation. Disadvantage: The applicable surface is more limited than HQL, not recommended.


SQL query

static List SQL () {

Session s = hibernateutil.getsession ();
Query q = S.createsqlquery ("SELECT * from User"). Addentity (User.class);
List<user> rs = q.list ();
S.close ();
Return RS;
}

Application: Not familiar with hql friends, and do not intend to transfer database platform friends, omnipotent method shortcomings: the destruction of cross-platform, difficult to maintain, not object-oriented.

Named queries

static List namedquery (int id) {
Session s = hibernateutil.getsession ();
Query q = s.getnamedquery ("Getuserbyid");
Q.setinteger ("id", id);
return Q.list ();
}

<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<class name= "Com.sy.vo.User" table= "User" catalog= "News" >



</class>
<!--named queries: Defining query Conditions--
<query name= "Getuserbyid" >
<! [Cdata[from User where id=:id]]>
</query>
< using SQL in!--named queries, deprecated, affecting cross-database
<sql-query name= "GetUserById2" >
<! [Cdata[select * from User where]]>
</sql-query>

Application: The universal method, a bit like the Ibatis lightweight frame operation, convenient maintenance. Cons: Not object-oriented. Based on HQL and SQL, there is a certain flaw.

Six ways to implement Hibernate queries, and IDE recommendations

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.