1. Configuration:

Summary: The Configuration class is responsible for managing Hibernate Configuration information. Start Hibernate and create the SessionFactory object.

(1) The underlying configuration information that Hibernate runs: database URL, user name, password, JDBC driver class, database Dialect, database connection pool, etc.

(2) Hibernate object relational mapping file (*.hbm.xml).

There are two ways to configure Hibernate:

(1) The properties file ((hibernate.properties) call code :Configuration CFG = new Configuration(); (2) XML file (hibernate.cfg.xml). Configuration CFG = new Configuration().configure(); The configrure() method reads hibernate.cfg.xml by default

2. The SessionFactory:

Summary: The application obtains the Session instance from the SessionFactory. It is shared between multiple application threads.

(1) SessionFactory is thread-safe, which allows multiple execution threads to access SessionFactory at the same time without data sharing problems. (2) The session factory caches the generated SQL statements and the mapping metadata that Hibernate uses at run time. (3) It is important to note that the SessionFactory is heavyweight, because in general, a project usually needs only one SessionFactory (singleton mode). When you need to operate more than one database, you can specify one SessionFactory for each database. SessionFactory = cfg.buildsSessionFactory ();

3.Session 

Summary: The Session interface is responsible for performing CRUD operations on the persisted object (CRUD’s task is to complete the communication with the database). (1) Session is also called the persistence manager, because it is the persistence related operation interface. (2) Session is not thread safe, and multiple threads should be avoided to share the same Session instance. (3) Session is opened through SessionFactory, and needs to be closed after all the work is finished. Hibernate 2 (Hibernate 2) ensures that there is only one Session instance in a thread and that getCurrentSession (Hibernate 3+) uses ThreadLocal to define the Session object. It has nothing to do with the Web tier’s HttpSession.

4. The Transaction (Transaction)

Summary: The Transaction interface is responsible for transaction-related operations. It abstracts application code from the underlying transaction implementation — this could be a JDBC transaction, a JTA user transaction, or even a Common Object Request Broker Architecture (CORBA) — and allows applications to control transaction boundaries through a consistent set of APIs.

This helps keep Hibernate applications portable across different types of execution environments or containers.

Call code: Transaction tx = session.beginTransaction(); tx.commit(); L Submit the transaction…. . tx.rollback(); Note: The call to Transaction (default: autoCommit=false) must be displayed when operating with Hibernate (add, delete, change).

5.Query

Summary: The Query interface allows you to execute queries on a database and control how the queries are executed. The query statements are written using HQL or the SQL dialect of the local database.

Query Query = session.createQuery(“fromUser”);

HibernateUtil. Java class is created under the package of cn.hrbust.dao

package cn.hrbust.dao;

import org.hibernate.HibernateException;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class HibernateUtil { private static Configuration cfg=null; private static SessionFactory sf=null; Static {// Try {CFG =new Configuration().configure(); sf=cfg.buildSessionFactory(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static SessionFactory getSessionFactory(){ return sf; SessionFactory} public void closesessionFactory (){sf.close(); // Close sessionFactory}} ManageUser to:

public class manageUser{ public static void main(String[] args){ Configuration cfg=null; SessionFactory sf=null; Session session=null; Transaction ts=null; User u=new User(); u.setName(“1814010833″); U.s etGender (” female “); u.setAge(21); u.setBirthday(Date.valueOf(“2000-2-5”)); try { sf=HibernateUtil.getSessionFactory(); Session = session.openSession (); // Session = session.openSession (); ts=session.beginTransaction(); session.save(u); ts.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); if(ts! =null){ ts.rollback(); } }finally{ session.close(); sf.close(); }}}

Hibernate3 recommends using getCurruntSession to open the session. For example: thread

"- / / Hibernate/Hibernate Configuration DTD / 3.0 / EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >

<hibernate-configuration>

<session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hellohibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.current_session_context_class">thread</property><! - ensure that each thread, speaking, reading and writing have a unique session instance - > < property name = "show_sql" > true < / property > < mapping resource = "cn/hrbust/pojo/User. HBM. XML" / >  </session-factory>

> To the session = sf getCurrentSession ();

public class manageUser{ public static void main(String[] args){ Configuration cfg=null; SessionFactory sf=null; Session session=null; Transaction ts=null; User u=new User(); u.setName(“1814010833″); U.s etGender (” female “); u.setAge(21); u.setBirthday(Date.valueOf(“2000-2-5”)); try { sf=HibernateUtil.getSessionFactory(); // SessionFactory singleton =sf.getCurrentSession(); // Ensure that each reader thread has a unique session instance ts=session.beginTransaction(); session.save(u); ts.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); if(ts! =null){ ts.rollback(); }}finally{// When getCurrentSession is used, it automatically closes //session.close(); //sf.close(); }}}

3. If the transaction is not committed, it is only issued a statement, but the transaction is not committed, so the database does not have, so it must show the open transaction, that is, ts.com MIT must be added to be able to take effect

4.Query (1) Expansion function:

Save the user object:

public class manageUser extends TestCase{

public void testSavaUser(){

Configuration cfg=null; SessionFactory sf=null; Session session=null; Transaction ts=null; User u=new User(); U.setname (" ha ha ha "); U.s etGender (" male "); u.setAge(30); u.setBirthday(Date.valueOf("2000-2-5")); try { sf=HibernateUtil.getSessionFactory(); // SessionFactory singleton =sf.getCurrentSession(); // Ensure that each reader thread has a unique session instance ts=session.beginTransaction(); session.save(u); ts.commit(); } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); if(ts! =null){ ts.rollback(); } }finally{ //session.close(); //sf.close(); }}

}

 

(2) Query user object:

public class manageUser extends TestCase{

public void testQueryUser(){

Configuration cfg=null; SessionFactory sf=null; Session session=null; Transaction ts=null; try { sf=HibernateUtil.getSessionFactory(); // SessionFactory singleton =sf.getCurrentSession(); // Ensure that each reader thread has a unique session instance ts=session.beginTransaction(); Query query=session.createQuery("from User"); List<User> users =query.list();

// for(int I =0; i

        for(User u:users){
            System.out.println(u.getName()+" "+u.getAge());
        }
        
        ts.commit();
    } catch (HibernateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        if(ts!=null){
            ts.rollback();
            }
        }finally{
        //session.close();
        //sf.close();
        }
}

}