1.实体类Student
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.test.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Student { 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; } }
2.实体类Course
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.test.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity 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; } }
3.实体类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.test.model; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity 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(cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn(name="StudentId") public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER) @JoinColumn(name="CourseId") public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } }
4.测试类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.test.test; import org.hibernate.classic.Session; import com.test.Util.HibernateUtil; import com.test.model.Course; import com.test.model.Score; import com.test.model.Student; public class StudentManager { public static void main(String[] args) { new StudentManager().testSave(); } public void testSave() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Student s = new Student(); s.setName("张三"); Course c = new Course(); c.setName("语文"); Score sc = new Score(); sc.setScore(80); sc.setStudent(s); sc.setCourse(c); Student s1 = new Student(); s1.setName("李四"); Course c1 = new Course(); c1.setName("数学"); Score sc1 = new Score(); sc1.setScore(90); sc1.setStudent(s1); sc1.setCourse(c1); Student s2 = new Student(); s2.setName("王五"); Score sc2 = new Score(); sc2.setScore(70); sc2.setStudent(s2); sc2.setCourse(c1); session.save(sc); session.save(sc1); session.save(sc2); session.getTransaction().commit(); } }