Many-To-Many (学生、课程、分数)
来源:互联网 发布:安卓手机数据恢复 编辑:程序博客网 时间:2024/06/08 18:57
Many-To-Many (学生、课程、分数)
- 博客分类:
- Hibernate 4.1
manytomany
Many-To-Many (学生、课程、分数)
多对多:
一个学生可以选多门课
一门课可以被多个学生选修
多对一:
多组分数对应一个学生
多组分数对应一门课程
多对多要有三张表 分别为:student、course、sorce
一、Student.java
- 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;
- @Entity
- public class Student {
- private int id;
- private String name;
- private Set<Course> courses = new HashSet<Course>();
- @ManyToMany
- //设置第三张表score与student和course表之间的外键连接,操作其中两张表的数据时,成绩表数据也会被操作
- @JoinTable(name="score",
- joinColumns=@JoinColumn(name="student_id", referencedColumnName="id"),
- inverseJoinColumns=@JoinColumn(name="course_id", referencedColumnName="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;
- }
- }
二、Course.java
- 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;
- }
- }
三、Score.java
- 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="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="student_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;
- }
- }
- Many-To-Many (学生、课程、分数)
- Hibernate查询方式(many to many)
- many-to-many性能
- hibernate-----many-to-many
- Hibernate many to many
- hibernate many-to-many
- many to many
- Many-to-many Query
- Many-to-Many
- many-to-many
- Hibernate Many-to-Many
- NHibernate学习笔记(三):many-to-one/one-to-many/many-to-many关系映射
- NHibernate学习笔记(三):many-to-one/one-to-many/many-to-many关系映射
- Hibernate Many-To-Many Revisited
- hibernate many to many查询
- hibernate映射many-to-many
- grials many-to-many配置
- django:Many-to-many关系
- linux开发工具
- python 小技巧备注
- 清明节为我的C++扫墓,因为那里埋藏了我的历史!
- 实现一个搜索二叉树
- 搭建一个后台服务器--引子
- Many-To-Many (学生、课程、分数)
- Oracle常用操作
- Python中range和xrange的区别
- CSS 锁定TABLE表头
- USACO试题beads的两种解法
- 探求JavaScript 开发工具
- 继续——课本代码练习
- JS日期比较,使用正则表达式转换
- eclipse中单步调试