Three states of Hibernate objects

1. Object tristate in Hibernate

In Hibernate, objects have three states: temporary state (Transient), persistent state (Persistent) and free state (Detached).

Objects in persistent state are also called PO (PersistenceObject), and temporary objects and free objects are also called VO (ValueObject). 

1) Temporary state 

A java object whose memory space is opened up by the new command, for example:

User user=new User();

Temporary objects exist in isolation in memory. They are carriers of information and do not have any relationship with the data in the database.

(a) If there are no variables referencing the object, it will be recycled by gc;

(b) In Hibernate, the transient object can be associated with the database through the save() or saveOrUpdate() method of the session, and the corresponding data is inserted into the database. At this time, the temporary object is converted into a persistent object.



 

2.) Persistent state 

The object in this state has a corresponding record in the database and has a persistent identifier. The objects obtained through the session's get(), load() and other methods are persistent objects.

After the persistent object is modified, it will not be synchronized to the database immediately until the database transaction is committed. Before synchronization, persistent objects are dirty (Dirty).

(a) If the delete() method of hibernate is used, the corresponding persistent object becomes a temporary object. Since the corresponding data in the database has been deleted, the object is no longer associated with the records of the database.

(b) When a session executes close(), clear(), and evict(), the persistent object becomes a free object. At this time, although the object has a database identification value, it is no longer under the management of the HIbernate persistence layer.

Persistent objects have the following characteristics:

(1) Associated with the session instance;

(2) There are records associated with them in the database and have persistent identifiers.

 

3.) Free state

When the session associated with a persistent object is closed, the persistent object becomes a detached object. When the detached object is re-associated with the session, it becomes a persistent object again (changes during Detached will be persisted to the database ). The free object has the identification value of the database, but it is no longer within the scope of persistence management.

(a) Free objects can be transformed into persistent objects through methods such as update() and saveOrUpdate().

(b) If the delete() method of hibernate is used, the corresponding free object becomes a temporary object. Since the corresponding data in the database has been deleted, the object is no longer associated with the records of the database.

(c) when no variable references it, it will be recycled by gc when appropriate;

Free objects have the following characteristics:

(1) Essentially the same as a transient object, when no variables refer to it, the JVM will recycle it at an appropriate time;

(2) There is one more database record identification value than the transient object.

 

2, Hibernate source package structure

(To figure out the package structure, it is helpful to read the source code)

net.sf.hibernate.* 

The classes of this package are basically interface classes and exception classes

net.sf.hibernate.cache.*

Implementation class of JCS

net.sf.hibernate.cfg.*

Configuration file read class

net.sf.hibernate.collection.*

Hibernate collection interface implementation classes, such as List, Set, Bag, etc., the reason why Hibernate has to write its own collection interface implementation classes is to support lazy loading

net.sf.hibernate.connection.*

Provider for several database connection pools

net.sf.hibernate.dialect.*

Support a variety of database features, each Dialect implementation class represents a database, describes the data types and other features supported by the database, such as whether there is AutoIncrement, whether there is Sequence, whether there is paging SQL, etc.

net.sf.hibernate. eg.*

Example used in Hibernate documentation

net.sf.hibernate.engine.*

The class function of this package is relatively scattered

net.sf.hibernate.expression.*

HQL支持的表达式

net.sf.hibernate.hq.*

HQL实现

net.sf.hibernate. id.*

ID生成器

net.sf.hibernate.impl.*

最核心的包,一些重要接口的实现类,如Session,SessionFactory,Query等

net.sf.hibernate.jca.*

JCA支持,把Session包装为支持JCA的接口实现类

net.sf.hibernate.jmx.*

JMX是用来编写App Server的管理程序的,大概是JMX部分接口的实现,使得App Server可以通过JMX接口管理Hibernate

net.sf.hibernate.loader.*

也是很核心的包,主要是生成sql语句的

net.sf.hibernate.lob.*

Blob和Clob支持

net.sf.hibernate.mapping.*

hbm文件的属性实现

net.sf.hibernate.metadata.*

PO的Meta实现

net.sf.hibernate.odmg.*

ODMG是一个ORM标准,这个包是ODMG标准的实现类

net.sf.hibernate.persister.*

核心包,实现持久对象和表之间的映射

net.sf.hibernate.proxy.*

Proxy和Lazy Loading支持

net.sf.hibernate. ps.*

该包是PreparedStatment Cache

net.sf.hibernate.sql.*

生成JDBC sql语句的包

net.sf.hibernate.test.*

测试类,你可以用junit来测试Hibernate

net.sf.hibernate.tool.hbm2ddl.*

用hbm配置文件生成DDL

net.sf.hibernate.transaction.*

Hibernate Transaction实现类

net.sf.hibernate.type.*

Data types of properties of persistent objects defined in Hibernate

net.sf.hibernate.util. *

Some tool classes, the role is relatively scattered

net.sf.hibernate.xml.*

XML data binding

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327065529&siteId=291194637