package cn.edu.hpu.testSchool; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; @Entity @Table(name="s_student") public class Student { private int id; private String name; private Set<Course> courses=new HashSet<Course>(); @ManyToMany @JoinTable(name="s_score", joinColumns=@JoinColumn(name="student_id"), inverseJoinColumns=@JoinColumn(name="course_id") ) public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; } @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package cn.edu.hpu.testSchool; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="s_score") public class Score { private int id; private int score; private Student student; private Course course; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @ManyToOne @JoinColumn(name="studnet_id") public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @ManyToOne @JoinColumn(name="course_id") public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } }
package cn.edu.hpu.testSchool; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="s_course") public class Course { private int id; private String name; @Id @GeneratedValue public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
<mapping class="cn.edu.hpu.testSchool.Student"/> <mapping class="cn.edu.hpu.testSchool.Score"/> <mapping class="cn.edu.hpu.testSchool.Course"/>
@Test public void testSchoolAdd(){ cn.edu.hpu.testSchool.Student s=new cn.edu.hpu.testSchool.Student(); s.setName("zhangsan"); Course c=new Course(); c.setName("java"); Score score=new Score(); score.setCourse(c); score.setStudent(s); score.setScore(80); sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.openSession(); session.beginTransaction(); session.save(s); session.save(c); session.save(score); session.getTransaction().commit(); session.close(); }
@Test public void testSchoolLoad(){ sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory(); Session session=sessionFactory.openSession(); session.beginTransaction(); cn.edu.hpu.testSchool.Student s=(cn.edu.hpu.testSchool.Student)session.load(cn.edu.hpu.testSchool.Student.class, 2); for(Course c:s.getCourses()){ System.out.println(c.getName()); } session.getTransaction().commit(); session.close(); }
java
转载请注明出处:http://blog.csdn.net/acmman/article/details/43917081