Hibernate Getting Started (ii) Hibernate internal execution process, primary key generator, object state

Source: Internet
Author: User

Internal execution process

Primary key generator Identity (common)
1、表必须支持自动增长机制2、数据库生成主键3、不需要在程序中设置主键
Assigned
必须通过程序的方式给值才可以person.setId(xxx);一般用于开发的时候测试使用
Increment (Common)
1、如果选择该主键的生成方式,则必须是数字类型2、先获取主键的最大值,在最大值的基础上加1,形成新的主键3、效率比较低,因为这种方式会先select表中最大的主键值4、主键的生成是由hibernate内部实现的
Native
会根据不同的数据库选择不用的主键生成策略
Uuid
1、主键必须是varchar类型2、主键是由hibernate内部生成的3、在持久化类中必须是String类型4 自动生成,无需给值
Types of properties in a map file

There are two types of mapping files:

    string或者是java.lang.string,两种类型都可以使用,但是如果使用string类型的话,在映射到数据库的时候会先重string映射到java.lang.string类型,然后在映射到varchar类型,所以效率会相对比较低,所以推荐使用java.lang.string,    以此类推,比如date类型对应java.util.Date类型然后对应数据库中的DATE,TIME,TIMESTAMP类型
Three states of the object three state plots

Description

1. Objects such as the first-hand steps are called temporary state objects
2. The temporary state of the object does not interact with hibernate
3. When the fourth step is performed, the Pereson object interacts with the Hibernate container
If the primary key generation mechanism is increment, this time it will emit
Select Max (PID) finds the maximum value of the primary key
If the primary key generation mechanism is identity, an INSERT statement is issued
4. The object after the completion of the fourth is called the persistent object because it interacts with hibernate.
5, when the implementation of the 5th step, the completion of the transaction commit, this time hibernate session is not closed, so the object is persistent object, in the database has a corresponding value
6. When the 6th step is executed, the session is closed and the object becomes a de-tube object.
Out of Hibernate management

Object state Transition Plot

method to become persisted state get method

Pperson Person=person.get (person.class,1l);
This is the status of the person object that is persisted.

Update method

Why the Save method should be a persisted state object
一个对象必须成为持久化对象,在事务提交的时候,hibernate才能对该对象进行操作
Graphic

Removing a persisted state object from the Hibernate container evict method

Description: The evict method can remove an object from the Hibernate container
The UPDATE statement was not issued when the transaction was committed

Clear method

Description: The Session.clear method emptied the object inside hibernate

General examples

Description

      在事务提交的时候,hibernate会检查持久化对象,如果该对象的id还没有值,则会发出insert语句,如果该对象的id有值了,则再会检查快照,如果和快照不一样,则发出update语句,如果和快照一样,则什么都不做。      所以上面的例子中person虽然是持久化对象,但是快照中的值没有变,则不会发出update语句,person4被evict方法清理出了hibernate容易,变成了托管状态,person早数据库中没有id值,所以发出insert语句,person有id值,但是值发生了改变,所以发出update语句

Hibernate Getting Started (ii) Hibernate internal execution process, primary key generator, object state

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.