개발관련/Spring
2015. 12. 7. 13:32
이번엔 1:N 관계를 살펴보자.
바로 소스를 보자.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package com.zest.hibernate.chapter7.onetomanymapping; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; @Entity public class College { @Id @GeneratedValue private int collegeId; private String collegeName; @OneToMany(targetEntity=Student.class, mappedBy="college" ,cascade= CascadeType.ALL,fetch=FetchType.EAGER) private List<Student> students ; public int getCollegeId() { return collegeId; } public void setCollegeId(int collegeId) { this.collegeId = collegeId; } public String getCollegeName() { return collegeName; } public void setCollegeName(String collegeName) { this.collegeName = collegeName; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package com.zest.hibernate.chapter7.onetomanymapping; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class Student { @Id @GeneratedValue private int studentId; private String studentName; @ManyToOne @JoinColumn(name="college_id") private College college; public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public College getCollege() { return college; } public void setCollege(College college) { this.college = college; } } | cs |
몇가지 새로운 어노테이션이 등장했다. @OneToMany, @ManyToOne
OneToMany가 선언되있는 클래스는 One을 지칭하고 targetEntity는 many가 된다.
mappedBy는 One에 속하는 테이블 명을 써주면 된다.
Student 객체는 ManyToOne을 선언하고 JoinColumn의 컬럼 명을 정의해주면 된다.
그럼 테스트 ㄱㄱ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package com.zest.hibernate.chapter7.onetomanymapping; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.Test; import com.zest.hibernate.chapter5.inheritancemapping.Module; import com.zest.hibernate.chapter5.inheritancemapping.Project; import com.zest.hibernate.chapter5.inheritancemapping.Task; public class OneToManyTest { @Test public void oneTest(){ AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(College.class); config.addAnnotatedClass(Student.class); config.configure("hibernate.cfg.xml"); new SchemaExport(config).create(true, true); // 아래 두개의 구문은 객체를 트랜잭션을 컨트롤. SessionFactory factory = config.buildSessionFactory(); Session session = factory.getCurrentSession(); session.beginTransaction(); College college1 = new College(); college1.setCollegeName("Newyork College"); Student s1 = new Student(); s1.setStudentName("Alex Rod"); Student s2 = new Student(); s2.setStudentName("Linda Berry"); s1.setCollege(college1); s2.setCollege(college1); session.save(college1); session.save(s1); session.save(s2); //session.save(alexdetail); 이건 안함. 카스케이드 라서 session.getTransaction().commit(); } } | cs |
결과는 다음과 같다.
1 2 3 4 | Hibernate: insert into TESTSCHEMA.College (collegeName, collegeId) values (?, ?) Hibernate: insert into TESTSCHEMA.Student (college_id, studentName, studentId) values (?, ?, ?) Hibernate: insert into TESTSCHEMA.Student (college_id, studentName, studentId) values (?, ?, ?) | cs |
예상했던 대로 College_ID는 FK로 셋팅이 된다.