3. Three statuses of hibernat

Source: Internet
Author: User

3. Three statuses of hibernat

Status chart:





Three statuses:

Transient (temporary)

The database does not exist. The session does not exist.

Persistent)

The database also exists in sessions.

Detached)

Session does not exist in the Database


The following examples show how to convert the three states and the number of SQL statements generated by each of them.

Temporary status

Example 1:

@ Testpublic void transient01Test () {Student student = new Student (); student. setStudentName ("Zhang San"); student. setAge (20 );}

Only a new object exists in the Database and session.


Persistence status

Example 2:

@ Testpublic void persistent01Test () {Student student = new Student (); student. setStudentName ("James"); student. setAge (20); Session session = null; try {session = HibernateUtil. openSession (); session. beginTransaction (); session. save (student); session. getTransaction (). commit ();} catch (HibernateException e) {e. printStackTrace (); session. getTransaction (). commit ();} finally {HibernateUtil. closeSession (session );}}

Student changes from a temporary state to a persistent state. It can be found that both the database and session exist. The premise of a session existence is that the session is not closed,

SQL statement sent:

Hibernate: insert into t_student (student_name, age, student_id) values (?, ?, ?)

Example 3:

@ Testpublic void persistent02Test () {Student student = new Student (); student. setStudentName ("James"); student. setAge (20); Session session = null; try {session = HibernateUtil. openSession (); session. beginTransaction (); session. save (student); student. setStudentName ("aa"); session. getTransaction (). commit ();} catch (HibernateException e) {e. printStackTrace (); session. getTransaction (). commit ();} finally {HibernateUtil. closeSession (session );}}

After the save () method is called, the student object is converted to a persistent object and exists in the session. when the transaction is committed, hibernate compares the current sudent object with the sudent in the session.

If the two are different, the student object in the session will be synchronized to the database and the update statement will be sent. The two are the same and will not be processed.

Send SQL statement:

Hibernate: insert into t_student (student_name, age, student_id) values (?, ?, ?)Hibernate: update t_student set student_name=?, age=? where student_id=?


Example 4:

@ Testpublic void persistent04Test () {Student student = new Student (); student. setStudentName ("James"); student. setAge (20); Session session = null; try {session = HibernateUtil. openSession (); session. beginTransaction (); session. save (student); student. setStudentName ("aa"); session. update (student); student. setAge (55); session. update (student); session. save (student); session. save (student); session. getTransaction (). commit ();} catch (HibernateException e) {e. printStackTrace (); session. getTransaction (). commit ();} finally {HibernateUtil. closeSession (session );}}

If a Persistent Object finds a change, calling update () or save () multiple times is invalid. It is synchronized to the database only when the transaction is committed and an update statement is sent.

Send SQL statement:

Hibernate: insert into t_student (student_name, age, student_id) values (?, ?, ?)Hibernate: update t_student set student_name=?, age=? where student_id=?

Free Status:

Example 5:

@Testpublic void detach01Test(){Student student=new Student();student.setStudentId(2);student.setStudentName("2b");student.setAge(22);Session session=null;try {session=HibernateUtil.openSession();session.beginTransaction();session.update(student);session.getTransaction().commit();} catch (HibernateException e) {e.printStackTrace();session.getTransaction().commit();}finally{HibernateUtil.closeSession(session);}}

In this case, studnet object with id 2 is in the Free State (database exists, session does not exist) and is converted to the persistent state by calling the update () method.

Send SQL statement:

Hibernate: update t_student set student_name=?, age=? where student_id=?

Example 6:

@Testpublic void detach02Test(){Student student=new Student();student.setStudentId(2);Session session=null;try {session=HibernateUtil.openSession();session.beginTransaction();session.delete(student);session.getTransaction().commit();} catch (HibernateException e) {e.printStackTrace();session.getTransaction().commit();}finally{HibernateUtil.closeSession(session);}}


In this case, the studnet object with id 2 is in the Free State (the database exists and the session does not exist) and is converted to a temporary state by calling the delete () method (the data does not exist or the session does not exist)


Send SQL statement:

Hibernate: delete from t_student where student_id=?




Example 7:


@Testpublic void detach03Test(){Session session=null;try {session=HibernateUtil.openSession();session.beginTransaction();Student student=(Student)session.get(Student.class,3);student.setStudentName("ddddd");session.getTransaction().commit();} catch (HibernateException e) {e.printStackTrace();session.getTransaction().commit();}finally{HibernateUtil.closeSession(session);}}

Call the get () method. student changes from the Free State to the persistent state. when the transaction is committed, student compares the current object.


Send SQL statement:

Hibernate: select student0_.student_id as student1_0_0_, student0_.student_name as student2_0_0_, student0_.age as age0_0_ from t_student student0_ where student0_.student_id=?Hibernate: update t_student set student_name=?, age=? where student_id=?


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.