Getting Started with Hibernate basics

Source: Internet
Author: User

1. What is Hibernate
Hibernate is a free open source framework, a or-mapping mapping tool that maps entity classes to database tables, is an excellent persistence layer solution, hibernate encapsulates JDBC, and we don't need to care about the underlying implementation. It only requires a relationship-specific business implementation.
      • Hibernate: Core classes and Interfaces
      • Configuration class: Loading the hibernate.cfg.xml config file
      • Sessionfactory Interface: Session factory, can get session
      • Session interface: Sessions, manipulating crud additions and deletions
      • Transaction interface: Transaction, open transaction, COMMIT TRANSACTION, close transaction

2.hibernate Working principle
Load the Hibernate.cfg.xml configuration file with the Config object,
The Hibernate.cfg.xml configuration file primarily manages the mapping of database connection related information to entity classes and database tables, so when loading the Hibernate.cfg.xml configuration file, a mapping is established between the entity class and the database table and then called Buildsessionfactory The () method obtains the session factory sessionfactory of the database connection, then obtains the session session through the Conversation factory, opens the transaction through session sessions, then executes the crud operation, commits the transaction, and finally closes the session.
3. Why Hibernate
Hibernate is an excellent persistence layer solution that provides a standardized template to improve developer productivity for developers. the difference between 4.hibernate and JDBC:
    • Query efficiency: JDBC Because of the direct operation of the database, so query efficiency compared to hibernate, hibernate to the entity class and database table field mapping relationship between the maintenance, so query efficiency is relatively low.
    • Development efficiency: JDBC is equivalent to manual, SQL code and encapsulation need to be done manually, and Hibernate is the equivalent of automation, because the JDBC is encapsulated, so the underlying code does not need to be written by developers, so the development efficiency hibernate is high.
What is the difference between 5.Hibernate and MyBatis?
      • Hibernate is a fully automated ORM framework and MyBatis is a semi-automatic ORM Framework
      • MyBatis is an ORM framework SQL statement and entity mapping based on DAO layer processing, Hibernate is a mapping of ORM framework tables and entities based on DAO layer processing
      • Hibernate is a heavyweight framework for medium to large projects, MyBatis is a lightweight framework for small and medium-sized projects, especially for current Internet projects
      • Hibernate package is better, and mybatis is more flexible

6. Primary KEY Query

    • What is the difference between a get query and a load query?
      • Get:session.get (Users.class, New Short ("4"));
      • Load:session.load (Users.class, New Short ("4"));
        Difference:
        • A get query that does not have data returns null and the load query does not throw an exception objectnotfoundexception
        • Hibernate uses lazy loading by default, and when no query data exists in the cache, get is found directly in the database, and load does not
        • Load will only produce a proxy object, and only then do anything other than get to actually query the database

Three states of Java objects in 7.Hibernate

Hibernate framework manages the state of Java objects by session

      • The transient state new object/delete is not stored to the database when it is created by new, and the state of the Java object is instantaneous.
      • Persistent state get, load/save/update, when the object is associated with the session and is managed by the session, he is in a persistent state
      • Free State evict, clear, close, in the persistent state of the object, separated from its associated session management, the object is in a free state.

After the session is managed, the object is in a free state. Session provides two methods (update () merge ()) to associate an object that is in a free State with a new session

The object is associated with the session:

      • Query interface via session, or get () |load ()
      • By invoking the session's Save () | Saveorupdate ()
8. Conversion between three states

Transient state transitions to persistent state

When an object is saved using the Save () or Saveorupdate () method of the Session object, the state of the object is converted from the transient state to the persistent state
Gets the object using the Get () or load () method of the Session object, which is the state of the persistent state that transitions to the transient state
After the Delete () method of the Session object is executed, the object changes from the original persistent state to the instantaneous state because the object is not associated with any database data at this time
Persistent state to Free State
The evict (), clear (), or close () method of the Session object is executed, and the object is shifted from the original persistent state to the Free State to the persistent state.
Retrieves the session object, executes the update () or Saveorupdate () method of the Session object, the object is moved from the Free State to the persistent state, and the object is associated with the session object again
The Free State transitions to instantaneous state
Executes the Delete () method of the Session object, and the object is shifted from the Free State to the instantaneous state.
Objects that are in a transient or Free State are no longer referenced by other objects and are processed by the Java Virtual Machine installation garbage collection mechanism.

9. Dirty Check and Refresh cache

    • Dirty Check: Object information in session is inconsistent with database
    • Refresh cache: Resolves the inconsistency between object information and database in session

  Implementation: Flush (), Commit ()

The session is the primary interface that hibernate provides to the application to manipulate the database, which provides a basic way to save, update, delete, and load Java objects. The session has a cache that manages and tracks all persisted objects, corresponding to related records in the database. At some point in time, the session executes the relevant SQL statement based on the changes in the object in the cache, updating the numbered data contained in the object to the database, a process known as refreshing the cache, in other words, refreshing the session cache synchronization to be consistent with the database.

Dirty Check
In Hibernate, objects that have changed before and after the state are called dirty objects. When a transaction commits, Hibernate detects the persistent state object in the session and changes the object's data when it is called a dirty check.
Why does hibernate have a dirty check?
Because if the object changes, you need to update the changes to the database to ensure that the objects in memory are consistent with the data in the database.

How does the session perform a dirty check?
When a Dept object is added to the session cache, the session copies a snapshot of the properties of the value type of the Dept object. When the session refreshes the cache, a dirty check is performed, which compares the current property of the Dept object with its snapshot to determine whether the properties of the Dept object have changed. If a change occurs, the session executes the relevant SQL statement based on the latest property values of the dirty object, updating the changes to the database.

Refresh Cache mechanism
When the properties of an object in the session cache change each time, the session does not immediately flush the cache and execute the associated SQL statement, but refreshes the cache at a specific point in time. This allows the session to combine several related SQL statements into one or a batch of SQL statements, reducing the number of accesses to the database, thereby improving the data access performance of the application.

When does the session flush the cache?
When the application calls Transcation's commit () method, the Commit () method first invokes the session's flush cache method Flush (), and then commits the transaction to the database. Hibernate schedules the refresh cache point at the end of the transaction, on the one hand because it reduces the frequency of access to the database, because the forest is able to minimize the time it takes for the current transaction to lock in the relevant resources in the database.

Refreshes the cache when the application displays the flush () method that invokes the session.
What is the difference between the flush () method of the session and the transaction commit () method?
The flush () method flushes the cached operation, executes a series of SQL statements, but does not commit the transaction, and the commit () method calls the Flush () method before committing the transaction. Committing a transaction means that the updates made to the database are permanently saved.




Getting Started with Hibernate basics

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.