Hibernate core and common technology

Source: Internet
Author: User

First, Hibernate introduction

1. Hibernate overview

Hibernate is a lightweight Java EE persistence Layer solution that manages the mapping of Java classes to database tables (ORM: Object-relational data mapping) and provides methods for retrieving data queries.

2. Architecture

  

(1) Sessionfactory: This is the key object of Hibernate, which is a compiled memory image of a single database mapping relationship, and it is thread-safe. It is the factory that generates the session itself to be applied to ConnectionProvider, which provides an optional level two cache of data that can be reused between those transactions at the level of the process and the cluster.

(2) Session: It is a single-threaded object that interacts between the application and the persistent storage layer. It is also a key object of Hibernate persistence, and all persistent objects must be managed by the session to be persisted. This object has a short lifetime, which hides the JDBC connection and is also a transaction factory. The session object has a first-level cache, and the data for all persisted operations is in the cache at the session object before the actual flush is performed.

(3) Persistent object: Once the system-created Pojo instance is associated with a specific session and corresponds to a specified record of the data table, the object is persisted, and this series of objects is called a persisted object. Modifications to persisted objects in the program are automatically converted to persistent layer modifications. Persistent objects can be just plain Java Beans/pojo, the only peculiarity being that they are associated with the session.

(4) transient and off-tube objects: The Java instance created by the system for the New keyword is not associated with a session and is in a transient state at this time. A transient instance may be an object that has not been persisted after being instantiated by the application. If an instance has been persisted today, it is converted to a de-tube state because of the closing of the session.

(5) transaction (Transaction): Represents an atomic operation, which has the concept of a database transaction. But it uses abstraction to separate applications from the underlying specific JDBC, JTA, and CORBA transactions. In some cases, a session may contain multiple transaction objects. Although transactional operations are optional, all persisted operations should be performed under transaction management, even if they are read-only.

(6) connection provider (ConnectionProvider): It is the factory that generates the JDBC connection and the connection pool. He isolates the underlying datasource and DriverManager through abstraction. This object does not have to be accessed directly by the application and is only used when the application needs to be expanded.

(7) Transaction Factory (Transactionfactory): He is the factory that generates instances of transaction objects. The object also does not require direct access to the application.

The core of Hibernate:

From there, we can see the six core interfaces of Hibernate, two main profiles, and their direct relationships. All of Hibernate's content is here. Well, let's go from top to bottom and make a brief summary of each interface.

1. Configuration interface: Responsible for configuring and starting Hibernate

2, Sessionfactory interface: responsible for initializing hibernate

3. Session interface: CRUD Operations for persistent objects

4. Transaction interface: Responsible for business

5, query interface and the criteria interface: responsible for the implementation of various database queries

Note: The configuration instance is an object during startup, and once Sessionfactory is created it is discarded.

3. Hibernate mapping

4. Component Attribute Mapping

5. Hibernate Association Mappings

Hibernate bidirectional Manytomany Mapping

7, HQL Query

Hibernate's HQL Query

8. Conditional query

Hibernate conditional Query

9. SQL query

Hibernate's SQL query

10. Transaction Management

SPRINGMVC+HIBERNATE4 Transaction Management Configuration

11. Caching mechanism

The purpose of caching is to reduce the interaction between the code and the database, thereby improving the access performance of the database. Hibernate is always enabled by default at the session level of the first level of cache (each session unique, when the session is closed after the cache is not), do not need the developer care, always valid. The sessionfactory level two cache is global and is shared by all sessions that are applied, and is turned off by default and is explicitly opened by the program. Once the two-level cache is turned on in the app, the session will look for a first-level cache when fetching the data, and then look for a level two cache, only to find the underlying database if there is no data to crawl.

The cached key is based on the SQL generated by the HQL, plus the parameters, pagination and other information

Caching introduction

1. Persisting the state of an object

Transient, persistent, off-pipe

Second, the question

1, Sessionfactory injection failure

Error message: No qualifying Bean of type ' org.hibernate.SessionFactory ' available

Workaround: Add in Web. xml

<listener>    <listener-class>       Org.springframework.web.context.ContextLoaderListener    </listener-class>  </ Listener>

2, Sessionfactory.getcurrentsession () cannot get synchronization transaction

Error message: [Request processing failed; Nested exception is org.hibernate.HibernateException:Could not obtain transaction-synchronized Session for current thread]

Workaround: Add in Web. xml

  <filter>    <filter-name>SpringOpenSessionInViewFilter</filter-name>    <filter-  Class>org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class>  </filter>  <filter-mapping>    <filter-name>springopensessioninviewfilter</ filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>

Springmvc4+hibernate4 run error could not obtain transaction-synchronized Session for current thread

"SSH Advanced Path" Hibernate fundamentals (i)

Hibernate core and common techniques

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.