|
@@ -1,112 +1,133 @@
|
1
|
|
-package kr.co.swh.lecture.opensource.hibernate.annotation;
|
2
|
|
-
|
3
|
|
-import java.util.Iterator;
|
4
|
|
-
|
5
|
|
-import org.hibernate.HibernateException;
|
6
|
|
-import org.hibernate.Session;
|
7
|
|
-import org.hibernate.SessionFactory;
|
8
|
|
-import org.hibernate.Transaction;
|
9
|
|
-
|
10
|
|
-/**
|
11
|
|
- * <pre>
|
12
|
|
- * kr.co.swh.lecture.opensource.hibernate.annotation
|
13
|
|
- * Query2.java
|
14
|
|
- *
|
15
|
|
- * 설명 : 하이버네이트 어노테이션 예제2 테스트
|
16
|
|
- * </pre>
|
17
|
|
- *
|
18
|
|
- * @since : 2017. 10. 26.
|
19
|
|
- * @author : tobby48
|
20
|
|
- * @version : v1.0
|
21
|
|
- */
|
22
|
|
-public class Query2 {
|
23
|
|
-
|
24
|
|
- private static SessionFactory sessionFactory;
|
25
|
|
-
|
26
|
|
- public Integer addPerson(String fname, String lname, int salary){
|
27
|
|
- Session session = sessionFactory.openSession();
|
28
|
|
- Transaction tx = null;
|
29
|
|
- Integer personID = null;
|
30
|
|
- try {
|
31
|
|
- tx = session.beginTransaction();
|
32
|
|
- Person person = new Person();
|
33
|
|
- person.setFirstName(fname);
|
34
|
|
- person.setLastName(lname);
|
35
|
|
- person.setSalary(salary);
|
36
|
|
- personID = (Integer) session.save(person);
|
37
|
|
- tx.commit();
|
38
|
|
- } catch (HibernateException e) {
|
39
|
|
- if (tx!=null) tx.rollback();
|
40
|
|
- e.printStackTrace();
|
41
|
|
- } finally {
|
42
|
|
- session.close();
|
43
|
|
- }
|
44
|
|
- return personID;
|
45
|
|
- }
|
46
|
|
-
|
47
|
|
- public void listPerson( ){
|
48
|
|
- Session session = sessionFactory.openSession();
|
49
|
|
- try {
|
50
|
|
- Iterator<Person> iterator = session.createQuery("FROM Person", Person.class).list().iterator();
|
51
|
|
- while(iterator.hasNext()){
|
52
|
|
- Person person = (Person) iterator.next();
|
53
|
|
- System.out.print("First Name: " + person.getFirstName());
|
54
|
|
- System.out.print("Last Name: " + person.getLastName());
|
55
|
|
- System.out.println("Salary: " + person.getSalary());
|
56
|
|
- }
|
57
|
|
- } catch (HibernateException e) {
|
58
|
|
- e.printStackTrace();
|
59
|
|
- } finally {
|
60
|
|
- session.close();
|
61
|
|
- }
|
62
|
|
- }
|
63
|
|
-
|
64
|
|
- public void updatePerson(Integer personId, int salary){
|
65
|
|
- Session session = sessionFactory.openSession();
|
66
|
|
- Transaction tx = null;
|
67
|
|
- try {
|
68
|
|
- tx = session.beginTransaction();
|
69
|
|
- Person person = (Person)session.get(Person.class, personId);
|
70
|
|
- person.setSalary(salary);
|
71
|
|
- session.update(person);
|
72
|
|
- tx.commit();
|
73
|
|
- } catch (HibernateException e) {
|
74
|
|
- if (tx!=null) tx.rollback();
|
75
|
|
- e.printStackTrace();
|
76
|
|
- } finally {
|
77
|
|
- session.close();
|
78
|
|
- }
|
79
|
|
- }
|
80
|
|
-
|
81
|
|
- public void deletePerson(Integer personId){
|
82
|
|
- Session session = sessionFactory.openSession();
|
83
|
|
- Transaction tx = null;
|
84
|
|
- try {
|
85
|
|
- tx = session.beginTransaction();
|
86
|
|
- Person person = (Person)session.get(Person.class, personId);
|
87
|
|
- session.delete(person);
|
88
|
|
- tx.commit();
|
89
|
|
- } catch (HibernateException e) {
|
90
|
|
- if (tx!=null) tx.rollback();
|
91
|
|
- e.printStackTrace();
|
92
|
|
- } finally {
|
93
|
|
- session.close();
|
94
|
|
- }
|
95
|
|
- }
|
96
|
|
-
|
97
|
|
- public static void main( String[] args ){
|
98
|
|
- sessionFactory = HibernateAnnotationUtil.getSessionFactory();
|
99
|
|
-
|
100
|
|
- Query2 query = new Query2();
|
101
|
|
- Integer value1 = query.addPerson("유키", "이", 1000);
|
102
|
|
- Integer value2 = query.addPerson("코야", "허", 5000);
|
103
|
|
- query.addPerson("SWH", "Academy", 10000);
|
104
|
|
- query.listPerson();
|
105
|
|
-
|
106
|
|
- query.updatePerson(value1, 5000);
|
107
|
|
- query.deletePerson(value2);
|
108
|
|
- query.listPerson();
|
109
|
|
-
|
110
|
|
- sessionFactory.close();
|
111
|
|
- }
|
112
|
|
-}
|
|
1
|
+package kr.co.swh.lecture.opensource.hibernate.annotation;
|
|
2
|
+
|
|
3
|
+import java.util.Iterator;
|
|
4
|
+
|
|
5
|
+import org.hibernate.HibernateException;
|
|
6
|
+import org.hibernate.Session;
|
|
7
|
+import org.hibernate.SessionFactory;
|
|
8
|
+import org.hibernate.Transaction;
|
|
9
|
+import org.hibernate.query.Query;
|
|
10
|
+
|
|
11
|
+/**
|
|
12
|
+ * <pre>
|
|
13
|
+ * kr.co.swh.lecture.opensource.hibernate.annotation
|
|
14
|
+ * Query2.java
|
|
15
|
+ *
|
|
16
|
+ * 설명 : 하이버네이트 어노테이션 예제2 테스트
|
|
17
|
+ * </pre>
|
|
18
|
+ *
|
|
19
|
+ * @since : 2017. 10. 26.
|
|
20
|
+ * @author : tobby48
|
|
21
|
+ * @version : v1.0
|
|
22
|
+ */
|
|
23
|
+public class Query2 {
|
|
24
|
+
|
|
25
|
+ private static SessionFactory sessionFactory;
|
|
26
|
+
|
|
27
|
+ public Integer addPerson(String fname, String lname, int salary){
|
|
28
|
+ Session session = sessionFactory.openSession();
|
|
29
|
+ Transaction tx = null;
|
|
30
|
+ Integer personID = null;
|
|
31
|
+ try {
|
|
32
|
+ tx = session.beginTransaction();
|
|
33
|
+ Person person = new Person();
|
|
34
|
+ person.setFirstName(fname);
|
|
35
|
+ person.setLastName(lname);
|
|
36
|
+ person.setSalary(salary);
|
|
37
|
+ personID = (Integer) session.save(person);
|
|
38
|
+ tx.commit();
|
|
39
|
+ } catch (HibernateException e) {
|
|
40
|
+ if (tx!=null) tx.rollback();
|
|
41
|
+ e.printStackTrace();
|
|
42
|
+ } finally {
|
|
43
|
+ session.close();
|
|
44
|
+ }
|
|
45
|
+ return personID;
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ public void listPerson( ){
|
|
49
|
+ Session session = sessionFactory.openSession();
|
|
50
|
+ try {
|
|
51
|
+ Iterator<Person> iterator = session.createQuery("FROM Person", Person.class).list().iterator();
|
|
52
|
+ while(iterator.hasNext()){
|
|
53
|
+ Person person = (Person) iterator.next();
|
|
54
|
+ System.out.print("First Name: " + person.getFirstName());
|
|
55
|
+ System.out.print("Last Name: " + person.getLastName());
|
|
56
|
+ System.out.println("Salary: " + person.getSalary());
|
|
57
|
+ }
|
|
58
|
+ } catch (HibernateException e) {
|
|
59
|
+ e.printStackTrace();
|
|
60
|
+ } finally {
|
|
61
|
+ session.close();
|
|
62
|
+ }
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ public void getPerson(String name){
|
|
66
|
+ Session session = sessionFactory.openSession();
|
|
67
|
+ try {
|
|
68
|
+ Query query = session.createQuery("FROM Person Where first_name=:name");
|
|
69
|
+ query.setParameter("name", name);
|
|
70
|
+ Iterator<Person> iterator = query.getResultList().iterator();
|
|
71
|
+ while(iterator.hasNext()){
|
|
72
|
+ Person person = (Person) iterator.next();
|
|
73
|
+ System.out.print("Find First Name: " + person.getFirstName());
|
|
74
|
+ System.out.print("Find Last Name: " + person.getLastName());
|
|
75
|
+ System.out.println("Find Salary: " + person.getSalary());
|
|
76
|
+ }
|
|
77
|
+ } catch (HibernateException e) {
|
|
78
|
+ e.printStackTrace();
|
|
79
|
+ } finally {
|
|
80
|
+ session.close();
|
|
81
|
+ }
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ public void updatePerson(Integer personId, int salary){
|
|
85
|
+ Session session = sessionFactory.openSession();
|
|
86
|
+ Transaction tx = null;
|
|
87
|
+ try {
|
|
88
|
+ tx = session.beginTransaction();
|
|
89
|
+ Person person = (Person)session.get(Person.class, personId);
|
|
90
|
+ person.setSalary(salary);
|
|
91
|
+ session.update(person);
|
|
92
|
+ tx.commit();
|
|
93
|
+ } catch (HibernateException e) {
|
|
94
|
+ if (tx!=null) tx.rollback();
|
|
95
|
+ e.printStackTrace();
|
|
96
|
+ } finally {
|
|
97
|
+ session.close();
|
|
98
|
+ }
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ public void deletePerson(Integer personId){
|
|
102
|
+ Session session = sessionFactory.openSession();
|
|
103
|
+ Transaction tx = null;
|
|
104
|
+ try {
|
|
105
|
+ tx = session.beginTransaction();
|
|
106
|
+ Person person = (Person)session.get(Person.class, personId);
|
|
107
|
+ session.delete(person);
|
|
108
|
+ tx.commit();
|
|
109
|
+ } catch (HibernateException e) {
|
|
110
|
+ if (tx!=null) tx.rollback();
|
|
111
|
+ e.printStackTrace();
|
|
112
|
+ } finally {
|
|
113
|
+ session.close();
|
|
114
|
+ }
|
|
115
|
+ }
|
|
116
|
+
|
|
117
|
+ public static void main( String[] args ){
|
|
118
|
+ sessionFactory = HibernateAnnotationUtil.getSessionFactory();
|
|
119
|
+
|
|
120
|
+ Query2 query = new Query2();
|
|
121
|
+ Integer value1 = query.addPerson("유키", "이", 1000);
|
|
122
|
+ Integer value2 = query.addPerson("코야", "허", 5000);
|
|
123
|
+ query.addPerson("SWH", "Academy", 10000);
|
|
124
|
+ query.listPerson();
|
|
125
|
+
|
|
126
|
+ query.updatePerson(value1, 5000);
|
|
127
|
+ query.deletePerson(value2);
|
|
128
|
+ query.listPerson();
|
|
129
|
+ query.getPerson("유키");
|
|
130
|
+
|
|
131
|
+ sessionFactory.close();
|
|
132
|
+ }
|
|
133
|
+}
|