Hibernate Summary (1), Hibernate Summary (
- Hibernate provides cache and snapshot mechanisms to improve performance.
Its cache can be classified into Level 1 cache and level 2 cache.
Hibernate level-1 cache: When an SQL statement is executed in a transaction, the returned results are stored in the Map set in the Session (of course, there are snapshots ).
- Test: (all the following code is in the try/catch Block)
Configuration config = new Configuration (). configure (); // The configure () method is to load src/hibernate. cfg. xml configuration file SessionFactory sf = config. buildSessionFactory (); Session s = sf. openSession (); // Session is a high-level encapsulation of Connection Transaction tran = null; try {tran = s. beginTransaction (); // The code here
Tran. commit ();} catch (HibernateException e) {if (tran! = Null) {tran. rollback () ;}e. printStackTrace () ;}finally {s. close (); sf. close ();}
- Query: including get (), load (), native SQL, HQL, Criteria (more object-oriented than HQL)
// 1.get(), load () method test User u = (User) s. get (User. class, 1); // The SQL statement generated during the first query, and the result is cached by User u1 = (User) s. get (User. class, 1); // No SQL statement is generated for the second query, but the result is taken from the cache p (u = u1); // true
// 2. HQL Query q = s. createQuery ("from domain. user where id = 1 "); User u2 = (User) q. uniqueResult (); // The SQL statement is generated for the third query, but the result is taken from the cache p (u2 = u); // true
// 3. native SQL SQLQuery q1 = s. createSQLQuery ("select * from User where id = 1"); q1.addEntity (User. class); User u3 = (User) q1.uniqueResult (); // The SQL statement is generated for the fourth query, but the result is taken from the cache p (u3 = u); // true
// 4. criteria query Criteria c = s. createCriteria (User. class); c. add (Restrictions. eq ("id", 1); User u4 = (User) q1.uniqueResult (); // an SQL statement is generated for the fifth query, however, the result is taken from the cache p (u4 = u); // true
Summary query:
| |
Get (), load () |
Native SQL, HQL, Criteria |
| SQL statement Cache |
√ |
X |
| Cache of the query object |
√ |
√ |
User user = new User (); // transient user of the object. setName ("xiaobai"); user. setAge (121); s. save (user); // The persistent state of the object
S. persist (user );
The difference between the two methods is that the primary key problem is set before the method is executed and the primary key problem is returned after the method is executed.
1. persist (): persistence of a transient instance. However, the identifier (the attribute corresponding to the identifier primary key) is not guaranteed to be immediately entered into the persistence instance, enter identifiers may be postponed to flush.
2. save (): a transient instance persistence identifier is generated in a timely manner. It must return the identifier, so it will immediately execute SQL insert.
User u = new User (); u. setName ("xiaobai"); u. setAge (121); s. save (u); // Insert the data into the database, convert the object to a persistent state, and save the returned object to the cache User u1 = (User) s. get (User. class, u. getId (); // No SQL statement is generated for this query. The result is taken from the Session cache p (u1 = u); // true
User u = (User) s. get (User. class, 10); // execute the query operation s. delete (u); // convert the persistent state of the object to the Free State
Of course, if you feel that you want to delete a data and perform query operations to reduce performance, you can do this:
User u=new User();u.setId(5);s.delete(u);
User u=(User) s.get(User.class, 1);u.setName("set");
However, sometimes we do not need to execute the s. update (object) method, which involves a feature of the object's persistent State (which also has a [snapshot] function ):
When an object is in a persistent State, when it updates data, the Framework compares the object with the previous snapshot. if the object is the same, no action is taken. If the object is different, it is automatically updated to the database.
// Of course, you can do the same.
User u = new User (); // the object's transient state. The automatic update function is not available. You need to manually update () u. setAge (1); u. setId (1); u. setName ("1"); s. update (u );
It is very important: although an SQL statement is formed in a transaction, the database will be operated only after the transaction. commit.
Hibernate needs to find out some things about database operations such as cache, snapshot, and object.
Object three states:
* Instantaneous State: it is not associated with hibernate and has no corresponding id in the database table.
* Persistent State: it is associated with hibernate and has the corresponding id --- OID in the database table.
* Free State: it is not associated with hibernate and has a corresponding id in the database table.