"SSH Tour" step-up Learning Hibernate framework (i): About persistence

Source: Internet
Author: User

without reference to any framework, we will continue to operate the database through mediocre code, resulting in a very large number of redundant but regular low-level code, so the frequent operation of the database and a large number of low-level code of repeated writing greatly wasted the program staff's writing. In such a case, the hibernate framework emerges.

In fact, Hibernate is packaged in model models, and this part of the package is called the persistence layer. The addition, deletion, updating, and querying of objects in this layer is called Persistence .

There are three types of persisted objects:Transient Objects,Persist Objects, Detached Objects.

Transient Objects:

Objects initialized with the new operation are not immediately persisted. Its state is instantaneous, not managed by the session, at this time, there is no matter what the database associated with the behavior, that is, the database does not have this record, only to be referenced by other objects, their status will be lost, and collected by the garbage collection mechanism.

The object is in this state under the following conditions:

(1) When an object has just been created through the new statement, it does not correspond to any record in the database.
(2) The Delete () method of the session can turn a persisted object or a free object into a temporary object. For a free object, the Delete () method deletes the corresponding record from the database, and for persisted objects. The Delete () method deletes the record corresponding to it from the database. and remove it from the session's cache.

Persist Objects:

Persistent instances are instances that have a database identity, regardless of what. It has persistent manager session Unified management. A persisted instance is manipulated in a transaction. Its state is synchronized with the database at the end of the transaction.

When a transaction is committed. The in-memory state is synchronized to the database by the INSERT, UPDATE, and DELETE statements that run SQL.

Many of the methods in the session can trigger a Java object to enter a persistent state, such as the session Save () to turn a temporary object into a persisted object. The object returned by load () or get () is in a persisted state. The list collection returned by find () holds all persisted objects. Update (), Saveorupdate (), and Lock () turn the free object into a persisted object.

Detached Objects:

When the session is closed, the persisted object becomes an offline object. Offline indicates that the object is no longer synchronized with the database and is no longer managed.

When the session's close () is called, the session's cache is cleared, and all persisted objects in the cache become free objects. Evict () can remove a persisted object from the cache and make it free.

Example Analysis:

Project structure:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvzgfuzgfuem1j/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Hibernate.cfg.xml file:

The configuration database information and the generated table

<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
User.hbm.xml file:

The configuration of the table that needs to be mapped.

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Hibernateutils class:

Read the previously configured Hibernate.cfg.xml, set up sessionfactory, session, close session, etc.

Package Zhudan.hibernate;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;import Org.hibernate.tool.hbm2ddl.schemaexport;public class HibernateUtils { private static Sessionfactory factory;static{try{//reads the hibernate.cfg.xmlConfiguration cfg=new Configuration (). Configure ();//Build Sessionfactoryfactory=cfg.buildsessionfactory ();} catch (Exception e) {e.printstacktrace ();}} Get sessionpublic static Session getsession () {return factory.opensession ();} Close sessionpublic static void CloseSession (Session session) {if (session!=null) {if (Session.isopen ()) {Session.close () ;}}} Sessionfactorypublic static Sessionfactory getsessionfactory () {return factory;}}
Exportdb Class: Mapping table. Generate the appropriate DDL for the Hbm.xml.

Package Zhudan.hibernate;import Org.hibernate.cfg.configuration;import Org.hibernate.tool.hbm2ddl.SchemaExport; public class Exportdb {public static void main (string[] args) {//Read Hibernate.cfg.xml file configuration cfg=new Configuration (). Configure (); Schemaexport export=new Schemaexport (CFG); Export.create (true, True);}}
Sessiontest class:

In this example, when new is a user object, the state isTransient Objects, this data is not in the database, is not managed by the session, when the runtime is saved. The state changes to Persist Objects, where the current data exists and is managed by the session, once the transaction is committed. After you run this statement. The status changes to Detached Objects, where this data exists in the database. Be managed by the session.


Persistence encapsulates the data access details. It provides an object-oriented interface for most business logic, reduces the number of database visits, adds an application's speed, makes it independent of the underlying database and upper-level business logic implementations, and replaces the database with only the configuration file changes without changing the code. Reusability is greatly improved.

"SSH Tour" step-up Learning Hibernate framework (i): About 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.