The main class of Hibernate, Configuration, SessionFactory

Configuration class

Class declaration:

public class Configuration  extends Object implements Serializable

First, the role of Configuration is to configure and start Hibernate. It includes:
The underlying information of Hibernate operation: database URL, user name, password, JDBC driver class, database Dialect, database connection pool. Hibernate mapping files (*.hbm.xml), etc.
During the startup process of Hibernate, the instance of the Configuration class first locates the location of the mapping document, reads the configuration and then creates a SessionFactory object.

Second, an org.hibernate.cfg.Configuration instance represents the complete set of Java types to SQL database mappings in an application. Configuration is used to build an immutable SessionFactory, and mapping definitions are compiled from different XML mapping definition files.

Two ways to configure Hibernate:

Properties file (hibernate.properties).
When we call:, Hibernate will automatically search the hibernate.properties file in the directory and read it into memory as the basic configuration for subsequent operationsConfiguration cfg = new Configuration();

Xml file (hibernate.cfg.xml).
When we call:, Hibernate will automatically search for the hibernate.cfg.xml file in the directory and read it into memory as the basic configuration for subsequent operationsConfiguration cfg = new Configuration().configure()//这里括号内是.xml文件的全类名;

Configuration loading method

There are three loading methods for configuration. Generally, we use hibernate.cfg.xml, which sets the properties of the database connection and the configuration of the hbm.xml mapping file. Hibernate will automatically load the configuration properties and automatically find the POJO. So to get the Configuration object, you just need to simply create the object.

Configuration cfg = new Configuration();
    cfg.configure(“config/hibernate.cfg.xml");

SessionFactory class

interface declaration

public interface SessionFactory extends Referenceable, Serializable
  1. The SessionFactory interface is responsible for initializing Hibernate. It acts as a proxy for the data storage source and is responsible for creating Session objects (SessionFactory is the factory that generates Sessions). The factory pattern is used here. In general, a project usually only needs one SessionFactory. When multiple databases need to be operated, one SessionFactory can be specified for each database.
  2. SessionFactory saves all the mapping relationships corresponding to the current database configuration, and is also responsible for maintaining the current secondary data cache.
  3. Hibernate4 has added a ServiceRegistry interface, all Hibernate-based configurations or services must be registered with this ServiceRegistry to take effect.
 Configuration configuration = new Configuration().configure();
 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
                 applySettings(configuration.getProperties()).buildServiceRegistry();
 SessionFactory sessionFactory =  configuration.buildSessionFactory(serviceRegistry);                                  

ServiceRegistry interface
ServiceRegistry is the registry of Service, which provides a unified loading/initialization/storage/acquisition mechanism for Service.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325769636&siteId=291194637