package com.test; 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 { private int collegeId; private String collegeName; private List<CollegeStudent> students; @Id @GeneratedValue 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; } @OneToMany(targetEntity = CollegeStudent.class, mappedBy = "college", cascade = CascadeType.ALL, fetch = FetchType.EAGER) public List<CollegeStudent> getStudents() { return students; } public void setStudents(List<CollegeStudent> students) { this.students = students; } }
CollegeStudent.java
package com.test; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity public class CollegeStudent { private int studentId; private String studentName; private College college; @Id @GeneratedValue 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; } @ManyToOne @JoinColumn(name = "college_id") public College getCollege() { return college; } public void setCollege(College college) { this.college = college; } }
CollegeStudentTest.java
package com.test; import static org.junit.Assert.*; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class CollegeStudentTest { static EntityManagerFactory entityManagerFactory; @BeforeClass public static void setUpBeforeClass() throws Exception { entityManagerFactory = Persistence .createEntityManagerFactory("org.hibernate.tutorial.jpa"); } @AfterClass public static void tearDownAfterClass() throws Exception { entityManagerFactory.close(); } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void test() { EntityManager entityManager = entityManagerFactory .createEntityManager(); entityManager.getTransaction().begin(); try { College college1 = new College(); college1.setCollegeName("newyork"); CollegeStudent student1 = new CollegeStudent(); student1.setStudentName("mike"); student1.setCollege(college1); entityManager.persist(student1); entityManager.persist(college1); College college2 = new College(); college2.setCollegeName("tjzd"); CollegeStudent student2 = new CollegeStudent(); student2.setStudentName("tom"); student2.setCollege(college2); entityManager.persist(student2); CollegeStudent student3 = new CollegeStudent(); student3.setStudentName("jerry"); student3.setCollege(college2); entityManager.persist(student3); entityManager.persist(college2); entityManager.getTransaction().commit(); } catch (Exception e) { entityManager.getTransaction().rollback(); } } @Test public void test02() { EntityManager entityManager = entityManagerFactory .createEntityManager(); entityManager.getTransaction().begin(); try { List<College> list = entityManager.createQuery("from College") .getResultList(); for (College college : list) { System.out.println(college.getCollegeName()); for (CollegeStudent student : college.getStudents()) { System.out.println("\t" + student.getStudentName()); } } entityManager.getTransaction().commit(); } catch (Exception e) { entityManager.getTransaction().rollback(); } } }
0 件のコメント:
コメントを投稿