|
|
@@ -1,48 +1,66 @@
|
|
1
|
|
-package kr.co.swh.lecture.opensource.hibernate.annotation;
|
|
2
|
|
-
|
|
3
|
|
-import java.util.HashSet;
|
|
4
|
|
-import java.util.Set;
|
|
5
|
|
-
|
|
6
|
|
-import org.hibernate.Session;
|
|
7
|
|
-import org.hibernate.SessionFactory;
|
|
8
|
|
-
|
|
9
|
|
-/**
|
|
10
|
|
- * <pre>
|
|
11
|
|
- * kr.co.swh.lecture.opensource.hibernate.annotation
|
|
12
|
|
- * Query1.java
|
|
13
|
|
- *
|
|
14
|
|
- * 설명 : 하이버네이트 어노테이션 예제1 테스트
|
|
15
|
|
- * </pre>
|
|
16
|
|
- *
|
|
17
|
|
- * @since : 2017. 10. 26.
|
|
18
|
|
- * @author : tobby48
|
|
19
|
|
- * @version : v1.0
|
|
20
|
|
- */
|
|
21
|
|
-public class Query1 {
|
|
22
|
|
- public static void main( String[] args ){
|
|
23
|
|
- SessionFactory sessionFactory = HibernateAnnotationUtil.getSessionFactory();
|
|
24
|
|
-
|
|
25
|
|
- Cart cart = new Cart();
|
|
26
|
|
- Items item1 = new Items(cart);
|
|
27
|
|
- Items item2 = new Items(cart);
|
|
28
|
|
- Set<Items> itemsSet = new HashSet<Items>();
|
|
29
|
|
- itemsSet.add(item1);
|
|
30
|
|
- itemsSet.add(item2);
|
|
31
|
|
- cart.setItems(itemsSet);
|
|
32
|
|
-
|
|
33
|
|
- Session session = sessionFactory.openSession();
|
|
34
|
|
- session.beginTransaction();
|
|
35
|
|
- session.save(cart);
|
|
36
|
|
- session.save(item1);
|
|
37
|
|
- session.save(item2);
|
|
38
|
|
- session.getTransaction().commit();
|
|
39
|
|
- System.out.println("Insert completed");
|
|
40
|
|
-
|
|
41
|
|
- System.out.println("Cart ID=" + cart.getId());
|
|
42
|
|
- System.out.println("item1 ID=" + item1.getId() + ", Foreign Key Cart ID=" + item1.getCart().getId());
|
|
43
|
|
- System.out.println("item2 ID=" + item2.getId() + ", Foreign Key Cart ID=" + item1.getCart().getId());
|
|
44
|
|
-
|
|
45
|
|
- session.close();
|
|
46
|
|
- sessionFactory.close();
|
|
47
|
|
- }
|
|
48
|
|
-}
|
|
|
1
|
+package kr.co.swh.lecture.opensource.hibernate.annotation;
|
|
|
2
|
+
|
|
|
3
|
+import java.util.HashSet;
|
|
|
4
|
+import java.util.Set;
|
|
|
5
|
+
|
|
|
6
|
+import org.hibernate.Session;
|
|
|
7
|
+import org.hibernate.SessionFactory;
|
|
|
8
|
+
|
|
|
9
|
+/**
|
|
|
10
|
+ * <pre>
|
|
|
11
|
+ * kr.co.swh.lecture.opensource.hibernate.annotation
|
|
|
12
|
+ * Query1.java
|
|
|
13
|
+ *
|
|
|
14
|
+ * 설명 : 하이버네이트 어노테이션 예제1 테스트
|
|
|
15
|
+ * </pre>
|
|
|
16
|
+ *
|
|
|
17
|
+ * @since : 2017. 10. 26.
|
|
|
18
|
+ * @author : tobby48
|
|
|
19
|
+ * @version : v1.0
|
|
|
20
|
+ */
|
|
|
21
|
+public class Query1 {
|
|
|
22
|
+ public static void main( String[] args ){
|
|
|
23
|
+ SessionFactory sessionFactory = HibernateAnnotationUtil.getSessionFactory();
|
|
|
24
|
+
|
|
|
25
|
+ Cart cart = new Cart();
|
|
|
26
|
+ Items item1 = new Items(cart);
|
|
|
27
|
+ Items item2 = new Items(cart);
|
|
|
28
|
+ Set<Items> itemsSet = new HashSet<Items>();
|
|
|
29
|
+ itemsSet.add(item1);
|
|
|
30
|
+ itemsSet.add(item2);
|
|
|
31
|
+ cart.setItems(itemsSet);
|
|
|
32
|
+
|
|
|
33
|
+ Session session = sessionFactory.openSession();
|
|
|
34
|
+ session.beginTransaction();
|
|
|
35
|
+ session.save(cart);
|
|
|
36
|
+ session.save(item1);
|
|
|
37
|
+ session.save(item2);
|
|
|
38
|
+ session.getTransaction().commit();
|
|
|
39
|
+ System.out.println("Insert completed");
|
|
|
40
|
+
|
|
|
41
|
+ System.out.println("Cart ID=" + cart.getId());
|
|
|
42
|
+ System.out.println("item1 ID=" + item1.getId() + ", Foreign Key Cart ID=" + item1.getCart().getId());
|
|
|
43
|
+ System.out.println("item2 ID=" + item2.getId() + ", Foreign Key Cart ID=" + item1.getCart().getId());
|
|
|
44
|
+
|
|
|
45
|
+ session.close();
|
|
|
46
|
+
|
|
|
47
|
+
|
|
|
48
|
+
|
|
|
49
|
+ session = sessionFactory.openSession();
|
|
|
50
|
+ session.beginTransaction();
|
|
|
51
|
+
|
|
|
52
|
+ cart = session.find(Cart.class, new Long(1));
|
|
|
53
|
+ session.getTransaction().commit();
|
|
|
54
|
+ System.out.println("Find completed");
|
|
|
55
|
+
|
|
|
56
|
+ System.out.println("Cart ID=" + cart.getId());
|
|
|
57
|
+ for(Items item : cart.getItems()) {
|
|
|
58
|
+ System.out.println("item1 ID=" + item.getId() + ", Foreign Key Cart ID=" + item.getCart().getId());
|
|
|
59
|
+ }
|
|
|
60
|
+
|
|
|
61
|
+ session.close();
|
|
|
62
|
+
|
|
|
63
|
+
|
|
|
64
|
+ sessionFactory.close();
|
|
|
65
|
+ }
|
|
|
66
|
+}
|