标签:style blog http io os ar java sp 数据
状态图:
三种状态:
Transient(临时状态)
数据库中不存在 session中不存在
Persistent(持久化状态)
数据库中存在 session中也存在
Detached(游离状态)
数据库中存在 session中不存在
通过下面的一些例子,来看看三个状态间的转化,以及各自产生的sql语句的条数
临时状态
例一:
持久化状态@Test public void transient01Test(){ Student student=new Student(); student.setStudentName("张三"); student.setAge(20); }
只是单纯的new一个对象,此时数据库与session中并不存在该对象
例二:
@Test <span style="white-space:pre"> </span>public void persistent01Test(){ <span style="white-space:pre"> </span>Student student=new Student(); <span style="white-space:pre"> </span>student.setStudentName("张三"); <span style="white-space:pre"> </span>student.setAge(20); <span style="white-space:pre"> </span>Session session=null; <span style="white-space:pre"> </span>try { <span style="white-space:pre"> </span>session=HibernateUtil.openSession(); <span style="white-space:pre"> </span>session.beginTransaction(); <span style="white-space:pre"> </span>session.save(student); <span style="white-space:pre"> </span>session.getTransaction().commit(); <span style="white-space:pre"> </span>} catch (HibernateException e) { <span style="white-space:pre"> </span>e.printStackTrace(); <span style="white-space:pre"> </span>session.getTransaction().commit(); <span style="white-space:pre"> </span>}finally{ <span style="white-space:pre"> </span>HibernateUtil.closeSession(session); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}
student由临时状态转为持久化状态,可以发现数据库及session中它都存在,session存在的前提是session未关闭,发送的sql语句:
Hibernate: insert into t_student (student_name, age, student_id) values (?, ?, ?)
例三:@Test public void persistent02Test(){ Student student=new Student(); student.setStudentName("张三"); 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); } }
由于调用save()方法后,student对象转化为持久化对象,在session中存在,当事务提交时,hibernate会将当前sudent对象与session中的sudent,进行比较如果两者不同,会将session中的student对象同步到数据库中,发送update语句,两者相同,不做处理.
发送sql语句:
Hibernate: insert into t_student (student_name, age, student_id) values (?, ?, ?) Hibernate: update t_student set student_name=?, age=? where student_id=?
例四:
@Test <span style="white-space:pre"> </span>public void persistent04Test(){ <span style="white-space:pre"> </span>Student student=new Student(); <span style="white-space:pre"> </span>student.setStudentName("张三"); <span style="white-space:pre"> </span>student.setAge(20); <span style="white-space:pre"> </span>Session session=null; <span style="white-space:pre"> </span>try { <span style="white-space:pre"> </span>session=HibernateUtil.openSession(); <span style="white-space:pre"> </span>session.beginTransaction(); <span style="white-space:pre"> </span>session.save(student); <span style="white-space:pre"> </span>student.setStudentName("aa"); <span style="white-space:pre"> </span>session.update(student); <span style="white-space:pre"> </span>student.setAge(55); <span style="white-space:pre"> </span>session.update(student); <span style="white-space:pre"> </span>session.save(student); <span style="white-space:pre"> </span>session.save(student); <span style="white-space:pre"> </span>session.getTransaction().commit(); <span style="white-space:pre"> </span>} catch (HibernateException e) { <span style="white-space:pre"> </span>e.printStackTrace(); <span style="white-space:pre"> </span>session.getTransaction().commit(); <span style="white-space:pre"> </span>}finally{ <span style="white-space:pre"> </span>HibernateUtil.closeSession(session); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>}
若一个持久化对象若发现变化后,调用多次update()或save()也是无效的,,只有事务提交时,才会同步到数据库,发送update语句发送sql语句:
Hibernate: insert into t_student (student_name, age, student_id) values (?, ?, ?) Hibernate: update t_student set student_name=?, age=? where student_id=?
游离状态:
例五:
@Test public 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); } }
此时id为2的studnet对象为游离状态(数据库中存在,session不存在),通过调用update()方法转化为持久化状态发送sql语句:
Hibernate: update t_student set student_name=?, age=? where student_id=?
例六:@Test public 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); } }
此时id为2的studnet对象为游离状态(数据库中存在,session不存在),通过调用delete()方法转化为临时状态(数据中不存在,session中也不存在)
发送sql语句:
Hibernate: delete from t_student where student_id=?
例七:
@Test public 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); } }
调用get()方法,student由游离状态变为持久化状态,当事务提交时会比较当前对象
发送sql语句:
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=?
标签:style blog http io os ar java sp 数据
原文地址:http://blog.csdn.net/wobendiankun/article/details/40382369