개발관련/Spring
2015. 12. 4. 16:59
이번엔 두개의 클래스를 하나의 테이블로 만드는 법을 살펴 보자.
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 | package com.zest.hibernate.chapter3; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.SecondaryTable; import javax.persistence.Table; @Entity public class School { @Id @GeneratedValue private int schoolId; private String schoolName; @Embedded private SchoolDetail schoolDetail; public SchoolDetail getSchoolDetail() { return schoolDetail; } public void setSchoolDetail(SchoolDetail schoolDetail) { this.schoolDetail = schoolDetail; } public int getSchoolId() { return schoolId; } public void setSchoolId(int schoolId) { this.schoolId = schoolId; } public String getSchoolName() { return schoolName; } public void setSchoolName(String schoolName) { this.schoolName = schoolName; } } | 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 | package com.zest.hibernate.chapter3; import javax.persistence.Embeddable; @Embeddable public class SchoolDetail { private String schoolAddress; private boolean isPublishSchool; private int studentCount; public String getSchoolAddress() { return schoolAddress; } public void setSchoolAddress(String schoolAddress) { this.schoolAddress = schoolAddress; } public boolean isPublishSchool() { return isPublishSchool; } public void setPublishSchool(boolean isPublishSchool) { this.isPublishSchool = isPublishSchool; } public int getStudentCount() { return studentCount; } public void setStudentCount(int studentCount) { this.studentCount = studentCount; } } | cs |
두개의 클래스가 있는데 새로운 어노테이션 발견....
부모가 되는 클래스는에는 @Embeded 라는 어노테이션을 쓰고 자식이 되는 클래스에는 @Embedable 어노테이션을 사용한다.
나머진 기존과 다를게 없다. 그럼 테스트코드. ㄱㄱ
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 | package com.zest.hibernate.chapter3; 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.chapter2.Customer; public class SchoolTest { @Test public void school1() { AnnotationConfiguration config = new AnnotationConfiguration(); config.addAnnotatedClass(School.class); config.configure("hibernate.cfg.xml"); new SchemaExport(config).create(true, true); // 아래 두개의 구문은 객체를 트랜잭션을 컨트롤. SessionFactory factory = config.buildSessionFactory(); Session session = factory.getCurrentSession(); session.beginTransaction(); SchoolDetail annsDetail= new SchoolDetail(); annsDetail.setPublishSchool(false); annsDetail.setSchoolAddress("101 washington"); annsDetail.setStudentCount(300); School stanns = new School(); stanns.setSchoolName("st. anns school"); stanns.setSchoolDetail(annsDetail); session.save(stanns); session.getTransaction().commit(); } } | cs |
테스트 코드는 두개의 객체를 사용하여 트랜잰션에 묶어준다. 그럼 결과 sql문을 보자.
1 2 | Hibernate: insert into TESTSCHEMA.School (isPublishSchool, schoolAddress, studentCount, schoolName, schoolId) values (?, ?, ?, ?, ?) | cs |
이런 문구가 나올것이다.