[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 running process of object-oriented software can be understood as constantly creating various new objects and establishing relationships between objects, call the object method to change the state of each object and the process of object extinction. No matter how the process and operation of the program run, it is essentially a result, the difference between the running result of the previous time point and the next time point in the program is manifested in a change in the object state in the memory. 2. to maintain the running state of the program when shutdown and memory space is insufficient, you need to save the object state in the memory to the persistent device and restore the object state from the persistent device, it is usually stored in a relational database to store a large amount of object information. In terms of the running function of Java programs, the function of saving the object state should be a very inconspicuous ancillary function compared to other functions running by the system. Java uses JDBC to implement this function, this inconspicuous function requires a lot of code, and the only thing to do is to save the object and restore the object, and the large amount of JDBC Code has no technical content, basically, it is a kind of hard work and repetitive work to use a set of standard code templates for routine business. 3. by using the database to save the objects and restore objects generated when Java programs are running, the ing between Java objects and relational database records is actually realized, which is called ORM (object relation mapping ), people can implement this function by encapsulating JDBC code. The encapsulated product is called The ORM framework, and Hibernate is a popular ORM framework. Using the Hibernate framework, you can save the object to a relational database by calling a save method without writing JDBC code. Instead, you can only call a get method, you can load an object from the database. 4. the basic process of using Hibernate is: configure the configuration object, generate sessionfactory, create Session object, start the transaction, complete the CRUD operation, submit the transaction, and close the session. 5. when using hibernate, you must first configure hibernate. cfg. XML file, in which the database connection information and dialect are configured, and the corresponding HBM is also configured for each object. XML file, hibernate. cfg. each HBM must be registered in the XML file. XML file. 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 a configeration class instance. 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 to copy all configuration information in the configeration object to the sessionfactory cache. 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: Small heavyweight objects: lightweight objects 3) method of calling sessionfactory to create Session 1. Users provide JDBC connections on their own. Connection con = datasource. getconnection (); Session S = sessionfactory. opensession (CON); 2. let sessionfactory provide connection session S = sessionfactory. opensession (); 4) uses 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, which 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: an external pluggable cache plug-in of sessionfactory. 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 objects in the database record how Java enters the temporary state 1. 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. persistence object database with OBJECT records 3. the session will keep the two synchronized at a specific time. How does Java enter the persistent state? 1. save () of the session to save the temporary-> persistent state 2. the object returned by the load () and get () Methods of the session 3. objects in the list set returned by the find () Operation of the session 4. session Update (), saveorupdate () to make free-> persistent 3. detached features: 1. no longer in session cache 2. the free object changes from the persistent state, and there may be corresponding records in the database. Java: how to enter the Persistence State-> free state 1. session close () method 2. Session evict () method, 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.

Dml@2012.12.7

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.