Repost six methods to implement hibernate query and IDE recommendation

Source: Internet
Author: User

These days are messy. Maybe it's because you have finished the test. Maybe something happened recently.
Right. I don't know myself anymore. I am very grateful to some people for their teaching. Because many things are of great help to me and
It inspired me to understand a lot of truth in addition to technology and programming. Maybe this is a mature process. I really hope
I hope I can forget the previous ignorance, the previous frivolous and practical technologies.

Sometimes, my "truth" is wrong, and my thoughts are sometimes "Naive ". I really want to be quiet. I saw it after dinner

An intern at Baidu wrote a log, and thought it was great to understand a definition of a master, or a realm or a kind

Advanced things.

I would like to start from now on, so that I can truly enjoy the joy of programming, no vanity, no competition, no distractions.
Only happiness, only enrichment, and regret-free. Of course, as I said, the log is about the philosophy of Starting from point to face and starting from face to point.
I think it is necessary to write a summary essay on hibernate. from point to point, I will talk about the six methods of Hibernate query. They areHQL Query
Object-based Criteria method, dynamic query of DetachedCriteria, example query, SQL query, and name query
.

If you simply use hibernate to query the database, you only need to understand one of them to complete the general functions you want to achieve,
From one point, let's master the method in Step 6 and provide more options. Each method has its applicability and prerequisites.

HQL Query

Hql is a query language of hibernate. It has the advantage of cross-database because of its different SQL syntax. Sample Code:

Static void query (String name ){
Session s = null;
Try {
S = HibernateUtil. getSession ();

// From is followed by an object, not a table name
String hql = "from Admin as admin where admin. aname =: name"; // use the name parameter. It is recommended and readable.
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 ();
}
}

 

Applicability: common methods, more traditional, similar to JDBC. Disadvantage: The new query language is applicable only to the Hibernate framework.

Object-based Criteria Query 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 ();
}
}

 

Applicability: object-oriented operations, revolutionizing the previous database operation methods and making it easy to read. Disadvantage: it is more suitable than hql.

Dynamic 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 return result:" + users );

 

Applicability: object-oriented operations, separating services from the underlying layer, without consuming field attributes to the DAO implementation layer. Disadvantage: it is more suitable 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;
}

 

Applicability: Object-oriented operations. Disadvantage: HQL is not recommended because it is more suitable than HQL.

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;
}

 

Applicability: friends who are not familiar with HQL and who are not planning to switch to the database platform. disadvantages of the omnipotent method: Damage cross-platform, difficult to maintain, and not object-oriented.

Name query

 

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>

<Hibernate-mapping>
<Class name = "com. sy. vo. User" table = "user" catalog = "news">

 

</Class>
<! -- Name query: Define query conditions -->
<Query name = "getuserbyid">
<! [CDATA [from user where id =: Id]>
</Query>
<! -- SQL is used in name query, which is not recommended and affects cross-Database
<SQL-query name = "getuserbyid2">
<! [CDATA [select * from user where]>
</SQL-query> -->
</Hibernate-mapping>

 

Applicability: a versatile method, which is similar to the ibatis Lightweight Framework for easy maintenance. Disadvantage: Not object-oriented. Based on hql and SQL, there are some defects.

 

* ************** Recommended for high-quality software: powerful Tool for netbeans6.5 Java and PHP developers *****************

Robbin recommended this IDE in programmer magazine, so I downloaded it yesterday, and I personally feel very useful. I think the following is a better example:

Netbeans6.5 (latest version) is a free ide tool, which is very helpful for promotion.

Based on JDK 5 and 6, Tomcat 6 and MySQL 5.

Supports plug-ins and a perfect development environment for new users.

Currently, J2SE, J2EE (JSF, EJB, Hibernate, Spring, JPA,
Development of PHP, Ruby, C/C ++, Groovy, JavaFX, js.

C/C ++ requires you to add your own configuration. Compilation is not supported by default. Ruby has a built-in parser by default.

Other support is pending.

Automatic prompts are provided, but the perception speed is slow and not strong enough.

Xml configuration file operations are intelligent and convenient for development.

The memory usage is less than 250 MB, and the software size is less than MB.

: Http://www.xdowns.com/soft/38/121/2007/soft_36782.html

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.