Hibernate second article [API explanation, execution flow chart]

Hibernate second article [API explanation, execution flow chart]

tags: Hibernate


Hibernate has been roughly introduced from the previous article and has a basis for a quick start case... This blog post mainly explains the Hibernate API

Let's take a look at what objects are used in the code of the quick start case, and then explain them one by one

    public static void main(String[] args) {

        User user = new User();

        Configuration configuration = new Configuration();


        SessionFactory factory = configuration.buildSessionFactory();

        Session session = factory.openSession();

        //Hibernate , 
        Transaction transaction = session.getTransaction();







Configuration management class: a class that mainly manages configuration files

It has a subclass AnnotationConfiguration , that is to say: we can use annotations instead of XML configuration files to configure the corresponding information

configure method

The configure() method is used to load the configuration file

  • How to load the main configuration file
    • If you specify a parameter, then load the path configuration file of the parameter
    • **If no parameters are specified, hibernate.cfg.xml in the src/directory will be loaded by default **

buildSessionFactory method

buildSessionFactory() is used to create Session factory


SessionFactory-->Session factory, can also be said to represent the hibernate.cfg.xml file ...hibernate.cfg.xml has <session-factory>such a node

openSession method

Create a Session object

getCurrentSession method

Create Session object or remove Session object


Session is the most important object of Hibernate. Session maintains a connection. As long as you use Hibernate to operate the database, you need to use the Session object.

Usually we will have the following methods in the DAO layer, Session also provides us with corresponding methods to achieve !

public interface IEmployeeDao {

	void save(Employee emp);
	void update(Employee emp);
	Employee findById(Serializable id);
	List<Employee> getAll();
	List<Employee> getAll(String employeeName);
	List<Employee> getAll(int index, int count);
	void delete(Serializable id);


Update operation

We used the save(Objcet o) method in the quick start, and calling this method saves the object in the database. The Session object also provides other methods to update the database

  • session.save(obj); [save an object]
  • session.update(obj); [Update an object]
  • session.saveOrUpdate(obj); Save or update method
    • **If the primary key is not set, execute save; **
    • **There is set the primary key to perform the update operation; **
    • If there is no error when setting the primary key!

Let's use the update() method.... Since it is an update operation, you must set the primary key. If the primary key is not set, how does the database know what you want to update. Modify the record with id 1 as follows:


Primary key query

Query the records of the database by the primary key, thereby returning a JavaBean object

  • session.get(javaBean.class, int id); [Inquire the corresponding class and id]
  • session.load(javaBean.class, int id); [Support lazy loading]

User rewrite toString() to see the effect:

       User user1 = (User) session.get(User.class, 1);


HQL query

HQL: hibernate query language is the object-oriented query language provided by hibernate

  • The query is the object and the object's attributes [it queries the object and the attribute, so it is case sensitive! .

SQL: Struct query language

  • The query is the table and column [case insensitive]

HQL is an object-oriented query language that can be used to query all data!

        Query query = session.createQuery("FROM User");

        List list = query.list();

Of course, it can also pass parameters to query

        Query query = session.createQuery("FROM User WHERE id=?");

        //0 JDBC 1 
        query.setParameter(0, user.getId());

        List list = query.list();


QBC query

QBC query: query by criteria fully object-oriented query

From the above HQL query, we can find that: HQL query requires the basis of SQL, because a small part of the SQL code still needs to be written ... QBC query is a complete object-oriented query ... But, we use it. a bit less

Let's take a look at how to use it:

        //user criteria 
        Criteria criteria = session.createCriteria(User.class);

        criteria.add(Restrictions.eq("id", 1));

        List list = criteria.list();

Local SQL query

Sometimes, if the SQL is very complex and we cannot rely on HQL queries to implement functions, we need to use native SQL to perform complex queries!

However, it has a flaw: it cannot be cross-platform...so we have already configured the "dialect" of the database in the main configuration file.

Let's use it briefly:

        //User List 
        SQLQuery sqlQuery = session.createSQLQuery("SELECT * FROM user").addEntity(User.class);

        List list = sqlQuery.list();



beginTransaction method

Open the transaction, the return is a transaction object... Hibernate stipulates that all database operations must be performed in a transaction environment, otherwise an error will be reported!

#Hibernate execution flow chart#

If you think this article helped you, you can give the author a little encouragement