tobby48 4 年前
父节点
当前提交
c7e3784d74

+ 59
- 53
src/main/java/kr/co/swh/lecture/opensource/hibernate/annotation/Cart.java 查看文件

@@ -1,54 +1,60 @@
1
-package kr.co.swh.lecture.opensource.hibernate.annotation;
2
-
3
-import java.util.Set;
4
-
5
-import javax.persistence.Column;
6
-import javax.persistence.Entity;
7
-import javax.persistence.GeneratedValue;
8
-import javax.persistence.GenerationType;
9
-import javax.persistence.Id;
10
-import javax.persistence.OneToMany;
11
-import javax.persistence.Table;
12
-
13
-/**
14
- * <pre>
15
- * kr.co.swh.lecture.opensource.hibernate.annotation
16
- * Cart.java
17
- *
18
- * 설명 : 하이버네이트 어노테이션 예제1
19
- * </pre>
20
- * 
21
- * @since : 2017. 10. 26.
22
- * @author : tobby48
23
- * @version : v1.0
24
- */
25
-@Entity
26
-@Table(name = "Cart")
27
-public class Cart {
28
-
29
-    @Id
30
-    @GeneratedValue(strategy = GenerationType.IDENTITY)
31
-    @Column(name = "cart_id")
32
-    private long id;
33
-
34
-
35
-    @OneToMany(mappedBy = "cart")
36
-    private Set<Items> items;
37
-
38
-    public long getId() {
39
-        return id;
40
-    }
41
-
42
-    public void setId(long id) {
43
-        this.id = id;
44
-    }
45
-
46
-
47
-    public Set<Items> getItems() {
48
-        return items;
49
-    }
50
-
51
-    public void setItems(Set<Items> items) {
52
-        this.items = items;
53
-    }
1
+package kr.co.swh.lecture.opensource.hibernate.annotation;
2
+
3
+import java.util.Set;
4
+
5
+import javax.persistence.Column;
6
+import javax.persistence.Entity;
7
+import javax.persistence.GeneratedValue;
8
+import javax.persistence.GenerationType;
9
+import javax.persistence.Id;
10
+import javax.persistence.OneToMany;
11
+import javax.persistence.Table;
12
+
13
+/**
14
+ * <pre>
15
+ * kr.co.swh.lecture.opensource.hibernate.annotation
16
+ * Cart.java
17
+ *
18
+ * 설명 : 하이버네이트 어노테이션 예제1
19
+ * </pre>
20
+ * 
21
+ * @since : 2017. 10. 26.
22
+ * @author : tobby48
23
+ * @version : v1.0
24
+ */
25
+@Entity
26
+@Table(name = "Cart")
27
+public class Cart {
28
+
29
+    @Id
30
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
31
+    @Column(name = "cart_id")
32
+    private long id;
33
+
34
+
35
+    @OneToMany(mappedBy = "cart")
36
+    private Set<Items> items;
37
+
38
+    public long getId() {
39
+        return id;
40
+    }
41
+
42
+    public void setId(long id) {
43
+        this.id = id;
44
+    }
45
+
46
+
47
+    public Set<Items> getItems() {
48
+        return items;
49
+    }
50
+
51
+    public void setItems(Set<Items> items) {
52
+        this.items = items;
53
+    }
54
+    
55
+    @Override
56
+    public boolean equals(Object obj) {
57
+    	// TODO Auto-generated method stub
58
+    	return super.equals(obj);
59
+    }
54 60
 }

+ 51
- 0
src/main/java/kr/co/swh/lecture/opensource/hibernate/annotation/CompositeCart.java 查看文件

@@ -0,0 +1,51 @@
1
+package kr.co.swh.lecture.opensource.hibernate.annotation;
2
+
3
+import javax.persistence.Column;
4
+import javax.persistence.EmbeddedId;
5
+import javax.persistence.Entity;
6
+import javax.persistence.Table;
7
+
8
+/**
9
+ * <pre>
10
+ * kr.co.swh.lecture.opensource.hibernate.annotation
11
+ * Cart.java
12
+ *
13
+ * 설명 : 하이버네이트 어노테이션 예제1
14
+ * </pre>
15
+ * 
16
+ * @since : 2017. 10. 26.
17
+ * @author : tobby48
18
+ * @version : v1.0
19
+ */
20
+@Entity
21
+@Table(name = "CompositeCart")
22
+public class CompositeCart {
23
+
24
+	@EmbeddedId
25
+    @Column(name = "pk")
26
+    private ItemKey id;
27
+    
28
+    @Column(name = "test")
29
+    private String uu;
30
+    
31
+    public CompositeCart() {
32
+		// TODO Auto-generated constructor stub
33
+	}
34
+
35
+    public ItemKey getId() {
36
+        return id;
37
+    }
38
+
39
+    public void setId(ItemKey id) {
40
+        this.id = id;
41
+    }
42
+
43
+	public String getUu() {
44
+		return uu;
45
+	}
46
+
47
+	public void setUu(String uu) {
48
+		this.uu = uu;
49
+	}
50
+
51
+}

+ 52
- 46
src/main/java/kr/co/swh/lecture/opensource/hibernate/annotation/HibernateAnnotationUtil.java 查看文件

@@ -1,47 +1,53 @@
1
-package kr.co.swh.lecture.opensource.hibernate.annotation;
2
-
3
-import org.hibernate.HibernateException;
4
-import org.hibernate.SessionFactory;
5
-import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
6
-import org.hibernate.cfg.Configuration;
7
-import org.hibernate.service.ServiceRegistry;
8
-
9
-/**
10
- * <pre>
11
- * kr.co.swh.lecture.opensource.hibernate.annotation
12
- * HibernateAnnotationUtil.java
13
- *
14
- * 설명 : 하이버네이트 어노테이션
15
- * </pre>
16
- * 
17
- * @since : 2017. 10. 26.
18
- * @author : tobby48
19
- * @version : v1.0
20
- */
21
-public class HibernateAnnotationUtil {
22
-	static SessionFactory sessionFactory;
23
-	static ServiceRegistry serviceRegistry;
24
-	
25
-	static{
26
-		try{
27
-			Configuration configuration = new Configuration().configure("hibernate-annotation.cfg.xml");
28
-			
29
-			//	예제1
30
-			configuration.addAnnotatedClass(Cart.class);
31
-			configuration.addAnnotatedClass(Items.class);
32
-			
33
-			//	예제2
34
-			configuration.addAnnotatedClass(Person.class);
35
-			
36
-			
37
-			serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
38
-			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
39
-		}catch(HibernateException e){
40
-			e.printStackTrace();
41
-		}
42
-	}
43
-	
44
-	public static SessionFactory getSessionFactory(){ 
45
-		return sessionFactory;
46
-	}
1
+package kr.co.swh.lecture.opensource.hibernate.annotation;
2
+
3
+import org.hibernate.HibernateException;
4
+import org.hibernate.SessionFactory;
5
+import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
6
+import org.hibernate.cfg.Configuration;
7
+import org.hibernate.service.ServiceRegistry;
8
+
9
+/**
10
+ * <pre>
11
+ * kr.co.swh.lecture.opensource.hibernate.annotation
12
+ * HibernateAnnotationUtil.java
13
+ *
14
+ * 설명 : 하이버네이트 어노테이션
15
+ * </pre>
16
+ * 
17
+ * @since : 2017. 10. 26.
18
+ * @author : tobby48
19
+ * @version : v1.0
20
+ */
21
+public class HibernateAnnotationUtil {
22
+	static SessionFactory sessionFactory;
23
+	static ServiceRegistry serviceRegistry;
24
+	
25
+	static{
26
+		try{
27
+			Configuration configuration = new Configuration().configure("hibernate-annotation.cfg.xml");
28
+			
29
+			//	예제1
30
+			configuration.addAnnotatedClass(Cart.class);
31
+			configuration.addAnnotatedClass(Items.class);
32
+			
33
+			//	예제2
34
+			configuration.addAnnotatedClass(Person.class);
35
+			
36
+			//	예제3
37
+			configuration.addAnnotatedClass(ItemKey.class);
38
+			configuration.addAnnotatedClass(CompositeCart.class);
39
+			
40
+			configuration.addAnnotatedClass(Result.class);
41
+			
42
+			
43
+			serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
44
+			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
45
+		}catch(HibernateException e){
46
+			e.printStackTrace();
47
+		}
48
+	}
49
+	
50
+	public static SessionFactory getSessionFactory(){ 
51
+		return sessionFactory;
52
+	}
47 53
 }

+ 62
- 0
src/main/java/kr/co/swh/lecture/opensource/hibernate/annotation/ItemKey.java 查看文件

@@ -0,0 +1,62 @@
1
+package kr.co.swh.lecture.opensource.hibernate.annotation;
2
+
3
+import java.io.Serializable;
4
+
5
+import javax.persistence.Embeddable;
6
+import javax.persistence.Entity;
7
+
8
+/**
9
+ * <pre>
10
+ * kr.co.swh.lecture.opensource.hibernate.annotation
11
+ * Cart.java
12
+ *
13
+ * 설명 : 하이버네이트 어노테이션 예제1
14
+ * </pre>
15
+ * 
16
+ * @since : 2017. 10. 26.
17
+ * @author : tobby48
18
+ * @version : v1.0
19
+ */
20
+@Embeddable
21
+public class ItemKey implements Serializable {
22
+
23
+    /**
24
+	 * 
25
+	 */
26
+	private static final long serialVersionUID = 1L;
27
+    
28
+	private int id;
29
+	private String name;
30
+	
31
+	public ItemKey() {
32
+		// TODO Auto-generated constructor stub
33
+	}
34
+	public ItemKey(int id, String name) {
35
+		// TODO Auto-generated constructor stub
36
+		this.id = id;
37
+		this.name = name;
38
+	}
39
+	
40
+	public int getId() {
41
+		return id;
42
+	}
43
+	public void setId(int id) {
44
+		this.id = id;
45
+	}
46
+	public String getName() {
47
+		return name;
48
+	}
49
+	public void setName(String name) {
50
+		this.name = name;
51
+	}
52
+	@Override
53
+	public boolean equals(Object obj) {
54
+		// TODO Auto-generated method stub
55
+		return super.equals(obj);
56
+	}
57
+	@Override
58
+	public int hashCode() {
59
+		// TODO Auto-generated method stub
60
+		return super.hashCode();
61
+	}
62
+}

+ 67
- 61
src/main/java/kr/co/swh/lecture/opensource/hibernate/annotation/Items.java 查看文件

@@ -1,62 +1,68 @@
1
-package kr.co.swh.lecture.opensource.hibernate.annotation;
2
-
3
-import javax.persistence.Column;
4
-import javax.persistence.Entity;
5
-import javax.persistence.GeneratedValue;
6
-import javax.persistence.GenerationType;
7
-import javax.persistence.Id;
8
-import javax.persistence.JoinColumn;
9
-import javax.persistence.ManyToOne;
10
-import javax.persistence.Table;
11
-
12
-/**
13
- * <pre>
14
- * kr.co.swh.lecture.opensource.hibernate.annotation
15
- * Items.java
16
- *
17
- * 설명 : 하이버네이트 어노테이션 예제1
18
- * </pre>
19
- * 
20
- * @since : 2017. 10. 26.
21
- * @author : tobby48
22
- * @version : v1.0
23
- */
24
-@Entity
25
-@Table(name = "Items")
26
-public class Items {
27
-
28
-    @Id
29
-    @GeneratedValue(strategy = GenerationType.IDENTITY)
30
-    @Column(name = "id")
31
-    private long id;
32
-
33
-
34
-    @ManyToOne
35
-    @JoinColumn(name = "cart_id", nullable = false)
36
-    private Cart cart;
37
-
38
-    // Hibernate requires no-args constructor
39
-    public Items() {
40
-    }
41
-
42
-    public Items(Cart c) {
43
-        this.cart = c;
44
-    }
45
-
46
-    public Cart getCart() {
47
-        return cart;
48
-    }
49
-
50
-    public void setCart(Cart cart) {
51
-        this.cart = cart;
52
-    }
53
-
54
-    public long getId() {
55
-        return id;
56
-    }
57
-
58
-    public void setId(long id) {
59
-        this.id = id;
60
-    }
61
-
1
+package kr.co.swh.lecture.opensource.hibernate.annotation;
2
+
3
+import javax.persistence.Column;
4
+import javax.persistence.Entity;
5
+import javax.persistence.GeneratedValue;
6
+import javax.persistence.GenerationType;
7
+import javax.persistence.Id;
8
+import javax.persistence.JoinColumn;
9
+import javax.persistence.ManyToOne;
10
+import javax.persistence.Table;
11
+
12
+/**
13
+ * <pre>
14
+ * kr.co.swh.lecture.opensource.hibernate.annotation
15
+ * Items.java
16
+ *
17
+ * 설명 : 하이버네이트 어노테이션 예제1
18
+ * </pre>
19
+ * 
20
+ * @since : 2017. 10. 26.
21
+ * @author : tobby48
22
+ * @version : v1.0
23
+ */
24
+@Entity
25
+@Table(name = "Items")
26
+public class Items {
27
+
28
+    @Id
29
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
30
+    @Column(name = "id")
31
+    private long id;
32
+
33
+
34
+    @ManyToOne
35
+    @JoinColumn(name = "cart_id", nullable = false)
36
+    private Cart cart;
37
+
38
+    // Hibernate requires no-args constructor
39
+    public Items() {
40
+    }
41
+
42
+    public Items(Cart c) {
43
+        this.cart = c;
44
+    }
45
+
46
+    public Cart getCart() {
47
+        return cart;
48
+    }
49
+
50
+    public void setCart(Cart cart) {
51
+        this.cart = cart;
52
+    }
53
+
54
+    public long getId() {
55
+        return id;
56
+    }
57
+
58
+    public void setId(long id) {
59
+        this.id = id;
60
+    }
61
+    
62
+    @Override
63
+    public boolean equals(Object obj) {
64
+    	// TODO Auto-generated method stub
65
+    	return super.equals(obj);
66
+    }
67
+
62 68
 }

+ 37
- 0
src/main/java/kr/co/swh/lecture/opensource/hibernate/annotation/Query3.java 查看文件

@@ -0,0 +1,37 @@
1
+package kr.co.swh.lecture.opensource.hibernate.annotation;
2
+
3
+import org.hibernate.Session;
4
+import org.hibernate.SessionFactory;
5
+
6
+/**
7
+ * <pre>
8
+ * kr.co.swh.lecture.opensource.hibernate.annotation
9
+ * Query1.java
10
+ *
11
+ * 설명 : 하이버네이트 어노테이션 예제1 테스트
12
+ * </pre>
13
+ * 
14
+ * @since : 2017. 10. 26.
15
+ * @author : tobby48
16
+ * @version : v1.0
17
+ */
18
+public class Query3 {
19
+	public static void main( String[] args ){
20
+        SessionFactory sessionFactory = HibernateAnnotationUtil.getSessionFactory();
21
+
22
+        CompositeCart cart = new CompositeCart();
23
+        ItemKey key = new ItemKey(1, "테스트입니다.");
24
+        cart.setId(key);
25
+        cart.setUu("abc");
26
+        
27
+        Session session = sessionFactory.openSession();
28
+        session.beginTransaction();
29
+        session.save(cart);
30
+        session.getTransaction().commit();
31
+        System.out.println("Insert completed");
32
+
33
+        session.close();
34
+        
35
+        sessionFactory.close();
36
+    }
37
+}

+ 94
- 0
src/main/java/kr/co/swh/lecture/opensource/hibernate/annotation/Query4.java 查看文件

@@ -0,0 +1,94 @@
1
+package kr.co.swh.lecture.opensource.hibernate.annotation;
2
+
3
+import java.util.Iterator;
4
+import java.util.List;
5
+
6
+import org.hibernate.HibernateException;
7
+import org.hibernate.Session;
8
+import org.hibernate.SessionFactory;
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 Query4 {
24
+
25
+	private static SessionFactory sessionFactory; 
26
+	
27
+
28
+	public void listPerson( ){
29
+		Session session = sessionFactory.openSession();
30
+		try {
31
+//			List<Result> list = session.createQuery("select a.id as cart_id, b.id as id, b.cart as item_cart_id from Cart a, Items b where a.id = b.cart", Result.class).getResultList();
32
+			List<Object> list = session.createQuery("select b from Cart a, Items b where a.id = b.cart", Object.class).getResultList();
33
+			Iterator<Object> iterator = list.iterator();
34
+			while(iterator.hasNext()){
35
+				Object o =  iterator.next(); 
36
+				Result person = (Result) iterator.next(); 
37
+				System.out.print("Cart_id: " + person.getCart_id()); 
38
+				System.out.print("Id: " + person.getId()); 
39
+				System.out.println("Item_cart_id: " + person.getItem_cart_id()); 
40
+			}
41
+		} catch (Exception e) {
42
+			e.printStackTrace(); 
43
+		} finally {
44
+			session.close(); 
45
+		}
46
+	}
47
+	
48
+	public void listPerson1( ){
49
+		Session session = sessionFactory.openSession();
50
+		try {
51
+//			Iterator<Result> iterator = session.createQuery("sinum.siname, sidogunnum.citynum, sidogunnum.sidoname from sinum as A, sidogunnum as B where A.areanum = B.areanum", Result.class).list().iterator();
52
+			Iterator<Object> iterator = session.createQuery("select sinum.siname, sidogunnum.citynum, sidogunnum.sidoname from sinum inner join sidogunnum on sinum.areanum = sidogunnum.areanum", Object.class).list().iterator();
53
+			while(iterator.hasNext()){
54
+				Result person = (Result) iterator.next(); 
55
+//				System.out.print("City Name: " + person.getCitynum()); 
56
+//				System.out.print("Sido Name: " + person.getSidoname()); 
57
+//				System.out.println("Si Name: " + person.getSiname()); 
58
+			}
59
+		} catch (Exception e) {
60
+			e.printStackTrace(); 
61
+		} finally {
62
+			session.close(); 
63
+		}
64
+	}
65
+	
66
+	public void getPerson(String name){
67
+		Session session = sessionFactory.openSession();
68
+		try {
69
+			Query query = session.createQuery("FROM CompositeCart Where test=:name");
70
+            query.setParameter("name", name);
71
+            Iterator<CompositeCart> iterator = query.getResultList().iterator();
72
+			while(iterator.hasNext()){
73
+				CompositeCart person = (CompositeCart) iterator.next(); 
74
+				System.out.print("uu: " + person.getUu()); 
75
+				System.out.print("id: " + person.getId()); 
76
+			}
77
+		} catch (HibernateException e) {
78
+			e.printStackTrace(); 
79
+		} finally {
80
+			session.close(); 
81
+		}
82
+	}
83
+
84
+	public static void main( String[] args ){
85
+		sessionFactory = HibernateAnnotationUtil.getSessionFactory();
86
+
87
+		Query4 query = new Query4();
88
+		
89
+		query.listPerson();
90
+		query.getPerson("abc");
91
+		
92
+		sessionFactory.close();
93
+	}
94
+}

+ 74
- 0
src/main/java/kr/co/swh/lecture/opensource/hibernate/annotation/Result.java 查看文件

@@ -0,0 +1,74 @@
1
+package kr.co.swh.lecture.opensource.hibernate.annotation;
2
+
3
+import java.util.Set;
4
+
5
+import javax.persistence.Column;
6
+import javax.persistence.Entity;
7
+import javax.persistence.GeneratedValue;
8
+import javax.persistence.GenerationType;
9
+import javax.persistence.Id;
10
+import javax.persistence.OneToMany;
11
+import javax.persistence.Table;
12
+
13
+/**
14
+ * <pre>
15
+ * kr.co.swh.lecture.opensource.hibernate.annotation
16
+ * Cart.java
17
+ *
18
+ * 설명 : 하이버네이트 어노테이션 예제1
19
+ * </pre>
20
+ * 
21
+ * @since : 2017. 10. 26.
22
+ * @author : tobby48
23
+ * @version : v1.0
24
+ */
25
+public class Result {
26
+
27
+    @Column(name = "cart_id")
28
+    private long cart_id;
29
+    
30
+    @Column(name = "id")
31
+    private long id;
32
+    
33
+    @Column(name = "item_cart_id")
34
+    private long item_cart_id;
35
+
36
+    public Result() {
37
+		// TODO Auto-generated constructor stub
38
+	}
39
+    
40
+	public long getCart_id() {
41
+		return cart_id;
42
+	}
43
+
44
+	public void setCart_id(long cart_id) {
45
+		this.cart_id = cart_id;
46
+	}
47
+
48
+	public long getId() {
49
+		return id;
50
+	}
51
+
52
+	public void setId(long id) {
53
+		this.id = id;
54
+	}
55
+
56
+	public long getItem_cart_id() {
57
+		return item_cart_id;
58
+	}
59
+
60
+	public void setItem_cart_id(long item_cart_id) {
61
+		this.item_cart_id = item_cart_id;
62
+	}
63
+
64
+	@Override
65
+	public boolean equals(Object obj) {
66
+		// TODO Auto-generated method stub
67
+		return super.equals(obj);
68
+	}
69
+	@Override
70
+	public int hashCode() {
71
+		// TODO Auto-generated method stub
72
+		return super.hashCode();
73
+	}
74
+}

+ 2
- 1
src/main/resources/hibernate-annotation.cfg.xml 查看文件

@@ -10,7 +10,8 @@
10 10
 		<property name="hibernate.dialect">org.hibernate.dialect.MariaDB53Dialect</property>
11 11
 		<property name="hibernate.current_session_context_class">thread</property>
12 12
 		
13
-		<property name="hibernate.hbm2ddl.auto">create</property>
13
+		<property name="hibernate.hbm2ddl.auto">update</property>
14 14
 		<property name="hibernate.show_sql">true</property>
15
+		<property name="hibernate.format_sql">true</property>
15 16
 	</session-factory>
16 17
 </hibernate-configuration>