I recently learned the basic content of hibernate, which is used in projects. Basically, I want to know where to learn.
Today, let's take a look at six methods to implement hibernate queries .........
They areHql query, Object-based Query 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 the table name string hql = "from Admin as admin where admin. aname =: Name "; // use named parameters. Recommended and 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 ();}}
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 results:" + 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.
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 DTD ing DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
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.