[J2EE] basic concepts of Hibernate

Source: Internet
Author: User

The first time I came into contact with Hibernate was four years ago. Using myeclipse, I can directly generate object classes. I feel the magic of the framework. The most amazing thing is that I have never used Hibernate since I used ibatis.

I have to admit that when I get used to one way to deal with one type of things, I always try to face all the problems in the most habitual way until I have to change the method, this is the time.

Okay, no nonsense. Start to sort out Hibernate.

I. Basic knowledge about Hibernate:

1. The internal operation process of object-oriented software can be understood as constantly creating various new objects and creating objects.
Call object methods to change the state of each object and the process of object extinction, regardless of the process of running the program.
In essence, it is necessary to get a result that is different from the running result of the previous and next time points in the program.
The state of the objects in the memory has changed.
2. To maintain the running state of the program when shutdown and the memory space is insufficient, the object state in the memory must be kept
It is usually stored in a relational database to store large
Volume object information. In terms of the Java program running function, the function of saving the object state is more powerful than the system running function.
Can be said, it should be a very inconspicuous ancillary function, java uses jdbc to achieve this function, this inconspicuous
The function requires a lot of code, and the only thing to do is to save the object and restore the object, and a lot of jdbc
The Code has no technical content, and is basically written using a set of standard code templates for routine business.
And repetitive work.
3. Using the database to save the objects and restore objects generated when the java program is running is actually a java object and
Is the ing relationship of database records, called ORM (Object Relation Mapping). People can encapsulate JDBC
The encapsulated product is called The ORM framework. Hibernate is a popular product.
ORM framework. Using the Hibernate framework, you can simply call a save method without writing JDBC code.
To save an object to a relational database, you can just call a get method to load an object from the database.
4. The basic process for using Hibernate is to configure the Configuration object, generate SessionFactory, and create a session.
Object, start the transaction, complete the CRUD operation, submit the transaction, and close the session.
5. When using Hibernate, configure the hibernate. cfg. xml file, including the database connection information and dialect,
Configure the corresponding hbm. xml file for each object. You must register each hbm. xml file in the hibernate. cfg. xml file.
File. Www.2cto.com
6. When applying Hibernate, we should focus on understanding Session caching principles, cascade, delayed loading, and hql queries.

Read the above sentence carefully. If you do not understand it, we recommend that you study the Hibernate plan.

Ii. How Hibernate works

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. the user provides the JDBC connection.
Connection con = dataSource. getConnection ();
Session s = sessionFactory. openSession (con );
2. Enable SessionFactory to provide connections
Session s = sessionFactory. openSession ();
4) use various methods provided by the Session interface to manipulate database access.

Iii. Hibernate Cache System
Level 1 cache:
The Session has a built-in cache that stores the objects loaded by the current unit of work.
Each Session has its own independent cache and can only be accessed by the current unit of work.
Secondary cache:
SessionFactory's external pluggable cache plug-in. Data can be shared and accessed by multiple sessions.
SessionFactory's built-in cache: stores ing metadata and predefined SQL statements.

Iv. Java object status in Hibernate

1. temporary status (transient)
Features:
1. not in Session cache
2. No OBJECT records in the database
How to enter the temporary status of Java
1. When an object is created using the new statement
2. When the delete () method of the Session is called to delete an object from the Session cache.
2. persisted)
Features:
1. In Session cache
2. OBJECT records in the Persistent Object Database
3. The Session will be synchronized at a specific time.
How does Java enter the Persistence State?
1. save () of the Session to set the temporary-> persistent state
2. Objects returned by the load () and get () Methods of the Session
3. objects stored in the list set returned by the find () of the Session
4. update () and saveOrupdate () of the Session to make free-> persistent
3. detached)
Features:
1. It is no longer in Session cache
2. The free object changes from the persistent state, and there may be corresponding records in the database.
How does Java enter the persistent state-> free state?
1. Session close () method
2. The evict () method of the Session to delete an object from the cache. Improve performance. Use less.


Here we need to look at the Hibernate Persistent Object status chart:
 

5. Hibernate learning suggestions

Based on your own experience, the J2EE framework learning is still project-centered, and the framework just allows the project to be completed faster and better. After understanding this, you need to prepare relevant materials. e-books or libraries borrow a thick book that is not commonly used but can be used as dictionaries. If a function cannot be implemented normally, the solution is as follows:

Follow Other People's code -- Implement functions -- refactor code -- View principles -- Record experiences

In short, the premise of the learning framework is that it has a solid foundation and needs to improve work efficiency before it can be applied. Junior programmers suggest increasing their own effective code volume rather than getting too early into the mature framework.

 

 

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.