Hibernate working mechanism and common classes and Methods

Source: Internet
Author: User
Five Core classes or interfaces of Hibernate: (1) Configuration: Used to parse the hibernate. cfg. xml file and XXXXX. hbm. xml file, and create the SessionFactory object

Five Core classes or interfaces of Hibernate: (1) Configuration: Used to parse the hibernate. cfg. xml file and XXXXX. hbm. xml file, and create the SessionFactory object

Five Core classes or interfaces of Hibernate:
(1) Configuration: Used to parse the hibernate. cfg. xml file and XXXXX. hbm. xml file, and create the SessionFactory object. The Configuration object is used to configure and start Hibernate. The Hibernate application uses the Configuration instance to specify the location of the object-link ing file or dynamically configure the Hibernate attributes, and then create the SessionFactory instance.
(2) SessionFactory: initializes Hibernate, acts as a proxy for the data storage source, and creates a Session object. A SessinFactory instance corresponds to a data storage source, and the application obtains the Session instance from SessionFactory. If the application accesses multiple databases at the same time, how do I create a separate SessionFactory instance for each database.
(3) Session: Also known as the persistence manager for Object-level database operations.
Features:
1) It is not thread-safe. Therefore, when designing the software architecture, avoid multiple threads sharing the same Session instance.
2) The Session instance is lightweight. The so-called lightweight means that it does not need to consume too much resources to create or destroy it. This means that the Session object can be created or destroyed frequently in the program. For example, a separate Session instance can be assigned to each customer request or a separate Session instance can be assigned to each work unit.
3) every Session instance and a DB transaction are usually bound. That is to say, each execution of a DB transaction should first create a new Session instance, regardless of whether the transaction is successful or not, at last, you should call the close () method of the Session to release the resources occupied by the Session instance.
Note: Each Session instance has its own cache. the cache of this Session instance can only be accessed by the current unit of work.
(4) Query: queries the database. To use the HQL (HibernateQueryLanguage) query statement, the HQL query statement is object-oriented and references the class name and class attribute name.
(5) Transaction: used to manage operation transactions. It encapsulates the underlying transaction interfaces, including jdbc api, JTA (JavaTransactionAPI), and CORBA (CommonObjectRequestBroker Architecture) API.
3. Five Core classes or interfaces of Hibernate:
(1) Configuration: Used to parse the hibernate. cfg. xml file and XXXXX. hbm. xml file, and create the SessionFactory object. The Configuration object is used to configure and start Hibernate. The Hibernate application uses the Configuration instance to specify the location of the object-link ing file or dynamically configure the Hibernate attributes, and then create the SessionFactory instance.
(2) SessionFactory: initializes Hibernate, acts as a proxy for the data storage source, and creates a Session object. A SessinFactory instance corresponds to a data storage source, and the application obtains the Session instance from SessionFactory. If the application accesses multiple databases at the same time, how do I create a separate SessionFactory instance for each database.
(3) Session: Also known as the persistence manager for Object-level database operations.
Features:
1) It is not thread-safe. Therefore, when designing the software architecture, avoid multiple threads sharing the same Session instance.
2) The Session instance is lightweight. The so-called lightweight means that it does not need to consume too much resources to create or destroy it. This means that the Session object can be created or destroyed frequently in the program. For example, a separate Session instance can be assigned to each customer request or a separate Session instance can be assigned to each work unit.
3) every Session instance and a DB transaction are usually bound. That is to say, each execution of a DB transaction should first create a new Session instance, regardless of whether the transaction is successful or not, at last, you should call the close () method of the Session to release the resources occupied by the Session instance.
Note: Each Session instance has its own cache. the cache of this Session instance can only be accessed by the current unit of work.
(4) Query: queries the database. To use the HQL (HibernateQueryLanguage) query statement, the HQL query statement is object-oriented and references the class name and class attribute name.
(5) Transaction: used to manage operation transactions. It encapsulates the underlying transaction interfaces, including jdbc api, JTA (JavaTransactionAPI), and CORBA (CommonObjectRequestBroker Architecture) API.

Ii. Working Principle
1. Hibernate initialization.
Read Hibernate configuration information-> Create Session Factory
1) Create an instance of the Configeration class.
Its constructor is to read the configuration information (Hibernate config. xml) into the memory.
A Configeration instance represents a set of mappings from all Java classes of Hibernate to the SQL database.
2) create a SessionFactory instance
Copy all configuration information in the Configeration object to the cache of SessionFactory.
An instance of SessionFactory represents a database storage worker source, which is no longer associated with the Configeration object after being created.
Cache: the attribute of a Java object (usually a collection type Attribute-occupying memory space.
In the cache of SessionFactory: Hibernate configuration information. OR ing metadata.
Cache-large: heavyweight objects small: lightweight objects
3) Call SessionFactory to create a Session
1) Users provide their own JDBC connections.
Connection con = dataSource. getConnection ();
Session s = sessionFactory. openSession (con );
2) Enable SessionFactory to provide connections
Session s = sessionFactory. openSession ();

3) use various methods provided by the Session interface to manipulate database access.


Hibernate is a (1) middleware between applications and databases (2) Persistence tool that encapsulates the details of database access, it allows program developers to focus more on the processing of business logic (3) ORM tools and map Object-Oriented java to relational databases, the data in the memory and the data in the database can be synchronously updated in two directions. 1. the code for JDBC database access is encapsulated, which greatly simplifies the tedious and repetitive code at the data access layer.

2. Hibernate is a mainstream persistence framework based on JDBC and an excellent ORM implementation. He greatly simplifies the coding of the DAO layer.

3. hibernate uses the Java reflection mechanism instead of the bytecode enhancement program to achieve transparency.

4. hibernate has excellent performance because it is a lightweight framework. The flexibility of ing is excellent. It supports various relational databases, from one-to-one to many-to-many complex relationships.

How does hibernate query databases.
1) first create a pojo class (javaBean) (2) and then create a ing file (. hbm. xml) (3) create a configuration file (hibernate. hbm. xml) (4) Finally, call the Hibernate API to perform CRUD operations on the database.

Hibernate

Working principle:

1. Read and parse the configuration file

2. Read and parse the ing information and create SessionFactory

3. Open Sesssion

4. Create transaction Transation

5. Persistent operations

6. Submit the transaction

7. Disable Session

8. Disable SesstionFactory
Workflow:
(1) first create a pojo class (javaBean)
(2) create a ing file (. hbm. xml)
(3) create a configuration file (hibernate. hbm. xml)
(4) Finally, call the Hibernate API to perform CRUD operations on the database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.