JAvaステータスオブジェクト
17990 ワード
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 }