1 package cn.itcast.hibernate.sh.state;
2
3 import org.hibernate.Session;
4 import org.hibernate.Transaction;
5 import org.junit.Test;
6
7 import cn.itcast.hibernate.sh.domain.Person;
8 import cn.itcast.hibernate.sh.utils.HiberanteUtils;
9
10 public class StateTest extends HiberanteUtils{
11 /**
12 * session.save方法把一个临时状态的对象转化成持久化状态的对象
13 */
14 @Test
15 public void testSavePerson(){
16 Session session = sessionFactory.openSession();
17 Transaction transaction = session.beginTransaction();
18 Person person = new Person();
19 person.setPname("afds");
20 person.setPsex("af");
21 session.save(person);
22 transaction.commit();
23 session.close();
24 }
25
26 /**
27 * session.update方法可以把一个对象的状态变成持久化状态
28 */
29 @Test
30 public void testUpdate(){
31 Session session = sessionFactory.openSession();
32 Transaction transaction = session.beginTransaction();
33 Person person2 = new Person();//临时状态
34 person2.setPid(1L);//临时状态
35 session.update(person2);//持久化状态
36 transaction.commit();
37 session.close();
38 }
39
40 /**
41 * 当session.get方法得到一个对象的时候,是不需要再执行 update语句,因为已经是持久化状态
42 * 当一个对象是一个持久化对象的时候,当进行提交的时候,hibernate内部会让该对象和快照进行对比,如果一样,则不发出update语句
43 * 如果不一样,则发出update语句
44 */
45 @Test
46 public void testGet(){
47 Session session = sessionFactory.openSession();
48 //Transaction transaction = session.beginTransaction();
49 Person person = (Person)session.get(Person.class, 1L);//持久化
50 person.setPname("tt");
51 //session.update(person);
52 //transaction.commit();
53 session.close();
54 }
55
56 /**
57 * session.clear方法把所有的对象从session中清空
58 */
59 @Test
60 public void testClear(){
61 Session session = sessionFactory.openSession();
62 Transaction transaction = session.beginTransaction();
63 Person person = (Person)session.get(Person.class, 1L);
64 person.setPname("asd");
65 session.clear();//把session中所有的对象清空
66 session.update(person);//把对象从脱管状态转化成持久化状态
67 transaction.commit();
68 session.close();
69 }
70
71 /**
72 * session.evict把一个对象从session中清空
73 */
74 @Test
75 public void testEvict(){
76 Session session = sessionFactory.openSession();
77 Transaction transaction = session.beginTransaction();
78 Person person = (Person)session.get(Person.class, 1L);
79 person.setPname("asdsss");
80 session.evict(person);//脱管状态
81 session.update(person);//把对象的状态转化成持久化状态
82 transaction.commit();
83 session.close();
84 }
85
86
87 /**
88 * 一个对象是否是持久化对象是针对某一个session而言的
89 */
90 @Test
91 public void testSession(){
92 Session session = sessionFactory.openSession();
93 Transaction transaction = session.beginTransaction();
94 Person person = (Person)session.get(Person.class, 1L);
95 person.setPname("asfd");
96 transaction.commit();
97 session.close();
98 session = sessionFactory.openSession();
99 transaction = session.beginTransaction();
100 person.setPname("aaaaaaa");//person对象对于94的session来说是一个临时状态的对象
101 //session.update(person);
102 transaction.commit();
103 session.close();
104 }
105
106 /**
107 * 当执行transaction.commit的时候,hibernate内部会检查session
108 * 1、如果一个对象为临时状态的对象,则session不会管
109 * 2、如果一个对象是持久化状态的对象,如果有ID值,并且和数据库对应,那么先把该对象与快照进行对比,如果一致,则什么都不做,如果不一致,则发出update语句
110 * 3、如果一个对象是持久化状态的对象,如果没有ID值,则会发出save语句
111 */
112 @Test
113 public void testMuplyOption(){
114 Session session = sessionFactory.openSession();
115 Transaction transaction = session.beginTransaction();
116 Person person = new Person();
117 person.setPname("rrr");
118 person.setPsex("asdfasdf");
119
120 Person person3 = new Person();
121 person3.setPname("rrr");
122 person3.setPsex("asdfasdf");
123
124
125 session.save(person);
126 Person person2 = (Person)session.get(Person.class, 1L);
127 person2.setPname("afds45");
128 transaction.commit();
129 session.close();
130 }
131
132
133 @Test
134 public void testMutiplyOption2(){
135 Session session = sessionFactory.openSession();
136 Transaction transaction = session.beginTransaction();
137 Person person = new Person();
138 person.setPname("asdfads");
139 person.setPsex("er");
140 session.save(person);
141 transaction.commit();
142 session.evict(person);
143 session.close();
144
145
146 session = sessionFactory.openSession();
147 transaction = session.beginTransaction();
148 session.update(person);
149 session.clear();
150 transaction.commit();
151 session.close();
152 }
153 }
时间: 2024-10-13 16:06:22