How JAVA Hibernate works and why it is used (go)

Source: Internet
Author: User

about Hibernate:
Hibernate is an open source framework, which is a framework for object-relational mapping, and it is a lightweight encapsulation of JDBC, and our Java programmers can manipulate the database using object-oriented thinking.
Hibernate Core Interface
Session: Responsible for persistent object CRUD operations
Sessionfactory: Responsible for initializing hibernate, creating Session Object
Configuration: Responsible for configuring and starting hibernate, creating sessionfactory
Transaction: Responsible for things-related operations
Query and Criteria interface: responsible for executing various database queries

How Hibernate works:

1. Through configuration config = new configuration (). Configure ();//Read and parse the Hibernate.cfg.xml config file
2. Read and parse the mapping information by <mapping resource= "Com/xx/user.hbm.xml"/> in Hibernate.cfg.xml
3. Via sessionfactory SF = Config.buildsessionfactory ();//Create Sessionfactory
4.Session Session = Sf.opensession ();//Open Sesssion
5.Transaction tx = Session.begintransaction ();//Create and start transaction transation
6.persistent operate operation data, persistent operation
7.tx.commit ();//Commit a transaction
8. Close session
9. Close Sesstionfactory

Why hibernate:
1. The code for JDBC access to the database is encapsulated, which greatly simplifies the tedious repetitive code of the data access layer.
2. Hibernate is a JDBC-based, mainstream persistence framework that is an excellent ORM implementation. He simplifies the coding of the DAO layer to a great extent
3. Hibernate uses the Java reflection mechanism rather than the bytecode enhancer to achieve transparency.
4. Hibernate performs very well because it is a lightweight framework. The flexibility of the mapping is excellent. It supports a variety of relational databases, from one-to-one to many-to-many complex relationships.

How does hibernate delay loading? The difference between get and load

1. For the hibernate get method, Hibernate confirms that the data exists for the ID, first in the session cache, then in the level two cache, without querying the database and returning NULL in the database. This is relatively simple, and there is not much controversy. The main point to note is that in this version (bibernate3.2 or more) The Get method will also find a level two cache!

2. Hibernate Load method when loading an entity object, based on the configuration of the lazy attribute at the class level on the mapping file (true by default), it is discussed in some cases:

(1) If true, first look in the session cache to see if the ID corresponds to the existence of the object, does not exist then use lazy loading, return the entity's proxy class object (the proxy class is a subclass of the entity class, generated dynamically by Cglib). Wait until the specific use of the object (in addition to the OID) to query the level two cache and database, if you still do not find a qualifying record, will throw a objectnotfoundexception.

(2) If False, it is the same as the Hibernateget method lookup order, but eventually if no qualifying records are found, a objectnotfoundexception will be thrown.

Here there are two important differences between get and load:

If a qualifying record is not found, the Hibernate get method returns NULL, and the load method throws a objectnotfoundexception.

The Load method returns an instance of a proxy class that does not have the Entity data loaded, and the Get method always returns an object with Entity data.

(For load and get method return types: "Get methods always return entity classes" is not actually correct, if the Get method finds the object corresponding to the ID in the session cache, if the object is previously proxied, if it was used by the Load method, or delayed by other associated objects, then the original proxy object is returned, not the entity class object, if the proxy object has not loaded the Entity data (that is, other than the ID of the property data), then it will query the level two cache or the database to load the data, but the return is the proxy object, Only the Entity data has been loaded. )

In short, for get and load the fundamental difference, in a word, hibernate for the load method that the data in the database must exist, can be assured that the use of agents to delay loading, if the use of the problem found in the process, can only throw exceptions, and for the Get method, Hibernate must obtain the actual data, otherwise null is returned.

How do the relationships between classes be implemented in hibernate? (e.g. one-to-many, many-to-many relationships)

The relationship between classes and classes is mainly manifested in the relationship between tables and tables, they operate on objects, and in our program we map all the tables and classes together, through the Many-to-one, One-to-many, Many-to-many,

The caching mechanism for hibernate is:

Hibernate caching: Hibernate is a persistent layer framework that accesses physical databases frequently to reduce the frequency of application access to physical data sources, thereby improving application performance.  The data in the cache is a copy of the data in the physical data source, the application reads and writes data from the cache at run time, or the event synchronizes the cache and the data Hibernate cache classification of the physical data source at a specific time: Hibernate caches consist of two main classes: Hibernate cache and Hibernate level two cache hibernate cache, also known as "Session Caching", which is built-in, meaning, You must use the session cache as long as you use Hibernate. Because the life cycle of a Session object usually corresponds to a database transaction or an application transaction, its cache is a transaction-scoped cache. In the first level cache, each instance of a persisted class has a unique OID. Hibernate level Two cache, also known as "Sessionfactory cache", is a process-wide or cluster-wide cache because the Sessionfactory object's life cycle corresponds to the entire process of the application, so hibernate level two caches are Concurrency problems are possible, so an appropriate concurrency access policy is required that provides the transaction isolation level for cached data. The second-level cache is optional and is a configurable plug-in, which, by default, is not enabled by Sessionfactory.   What data is suitable for storage in the second level cache?   1 rarely modified data 2 is not very important data, allowing for occasional concurrent data 3 data that will not be accessed concurrently 4 constant data is not suitable for storing to the second level of cached data? 1 frequently modified data 2. Data that is not allowed to be accessed concurrently, such as financial data, is not allowed to occur concurrently with 3 of data shared with other applications. How does hibernate find objects apply caching? When hibernate accesses the data object according to the ID, it is first checked from the session level cache, and if the level two cache is configured, then it is checked from the level two cache, and if it is not found, then the database is queried, and the result is placed in the cache to delete, update and add data by ID. Update cache hibernate to manage cache instances whenever we manage the hibernate cache (managing the caches), when you pass an object to the Save (), update (), or Saveorupdate () method, or using the load (), get (), list (), iterate (), or scroll () method to obtain an object, the pairThe image will be added to the internal cache of the session. When the flush () method is subsequently called, the state of the object is synchronized with the database.  If you do not want this synchronization to happen, or if you are working on a large number of objects and need to manage memory effectively, you can call the evict () method to remove these objects and their collections from the first-level cache.

how hibernate is queried
SQL, Criteria,object comptosition
HQL:
1. Attribute Query
2, parameter query, named parameter query
3. Related queries
4, paging query
5. Statistical functions

How to optimize hibernate?
1. Use bidirectional one-to-many associations without using
2. Flexible use of unidirectional one-to-many associations
3. Do not use one-to-many substitution
4. Configure the object cache without using the collection cache
5. One-to-many collection using bag, multi-set using Set
6. Inheriting classes using explicit polymorphism

7. Table fields are less, tables associated not afraid of more, there is a level two cache backing

Hibernate Development Steps:

Development steps
1) Setting up a good environment
Introducing Hibernate's Smallest jar package
Preparing the Hibernate.cfg.xml boot configuration file
2) Realistic body type (Pojo)
3) Write the mapping file "User.hbm.xml" for the entity class
Adding mapped entities in Hibernate.cfg.xml
4) Create a library table
5) Write Test class
Get Configuration
Create Sessionfactory
Open session
Open transaction
Use session to manipulate data
Commit a transaction
Close Resource

How JAVA Hibernate works and why it is used (go)

Related Article

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.