Hibernate how to achieve persistence

Source: Internet
Author: User

The interview was asked, maybe understood, but not clear.

Hibernate is a framework for ORM (object relation Mapping, objects relational mapping), so-called object-relational mapping, which, in layman's words, is to save Java objects in a relational database. What hibernate does is to project the object into a relational database and then implement persistence.  Its implementation: Establish the mapping of the object to the relational database.     Example: establishing a User class-to-database mapping a simple class User:public classes user{private int id;  private String name; } The user class corresponds to the database table user:column-name type rule ID number P name varchar2 R when making persistent , you need to save the value of the object's two attributes to the user in the database, and if you use JDBC, we need to write the SQL ourselves and get the values of the two properties of the object as arguments to persist. SQL might look like this: INSERT into USER (name) value (?); When using hibernate, there is no need to map the object to the table, using the configuration file to implement: User.hbm.xml: ... <class name= "user" table= "user" ><!-- This class object is saved to the user table--<id name= "id" column= "id" > <generator class= "Native"/><!--there are many strategies to generate IDs here, select this Automatically generate,--> </id> <property name= "name" column= "name" > </class&gt ... The mapping relationship is then established by configuring a join to the database through Hibernate.cfg.xml and reading the User.hbm.xml file. When you need to persist an object, you need to use the Session object to hibernate: Session.save (New User ()); see? We just need to manipulate the object on the line, that is, the programmer does not need to see the database, he only need to directly manipulate the object on the line. How does hibernate achieve persistence? A: 1. Hibernate is a framework for ORM (object relation Mapping, objects relational mapping), so-called object-relational mapping, which, in layman's words, is to save Java objects to a relational database, which is a framework for persistence. 2.hibernate persistence The first thing to do is to establish the mapping of the object to the relational database. 3.hibernate persists (saves) the object to the data inventory according to the defined mapping rules.  This enables object persistence.



Persistence is the name of the cache that is put in the database to make it persistent. For objects that need to be persisted, its life cycle is divided into three states: temporary state, persistent state, Free State. Temporary state: Just created with the new statement, not persisted, is not in the session's cache. A Java object that is in a temporary state is called a temporary object. Persistent state: It has been persisted and added to the session's cache. A persisted Java object is called a persisted object. Free State: Has been persisted, But not in the session cache, the free-State Java object becomes a free object. The characteristics of the persisted object:.  In the cache of a session case, it can be said that the persisted object is always associated with a session case.  persisted objects correspond to related records in the database.  When the session cleans up the cache, the database is updated synchronously based on the property changes of the persisted object.  The Save () method of the session turns the temporary state into a persistent state  . The session's update (), Saveorupdate (), and Lock () methods make the Free State a persistent state.


Hibernate how to achieve persistence

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.