057:hibernate:作业Student_Course_Score(重要)
来源:互联网 发布:组态读取串口数据 编辑:程序博客网 时间:2024/06/06 09:34
学生课程、分数的设计
使用联合主键@Embeddedled
实现serializable接口
package com.bjsxt.hibernate;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Course {private int id;private String name;@Id@GeneratedValuepublic 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 com.bjsxt.hibernate;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@GeneratedValuepublic 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;}}
package com.bjsxt.hibernate;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;@Entitypublic class Student {private int id;private String name;private Set<Course> courses = new HashSet<Course>();@ManyToMany@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@GeneratedValuepublic 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中,导行到Student
@ManyToOne
@JoinColumn(name="student_id")
public Student getStudent() {
return student;
}
导行到Course
@ManyToOne
@JoinColumn(name="course_id")
public Course getCourse() {
return course;
}
如果要从Student找到对应的哪些课程的话,把Student设到Course而不是把course设到Student,也就是manytomany的单向。
即设导航,通过Student可以导航到Course,通过Score可以导航到Student和Course
对Score.java中加上@Table(name="score")
运行结果:
0 0
- 057:hibernate:作业Student_Course_Score(重要)
- Hibernate Student_Course_Score设计
- hibernate--Student_Course_Score设计
- hibernate重要接口说明
- Hibernate重要规则总结
- Hibernate重要规则总结
- Hibernate重要规则总结
- hibernate重要查询语句
- Hibernate 重要几个注解
- hibernate重要笔记
- Hibernate--最佳实践 重要
- hibernate重要知识
- Hibernate重要对象的详解
- hibernate中几个重要对象
- SSIS 作业 发邮件 两个重要环节
- Hibernate中重要对象的详解
- Hibernate中重要对象的详解
- hibernate开发主键生成方式(重要)
- 一个xilinx IP的思考
- C++学习笔记(三):再探顺序容器
- Java多线程--生产者与消费者问题
- 0038分支项目合并到trunk中
- jQuery 实用代码
- 057:hibernate:作业Student_Course_Score(重要)
- JSP EL学习笔记
- 递归及递推系列之 汉诺塔III hdoj 2064
- edit control 2
- 工作笔记
- HDU4941Magical Forest (二分+链表优化)
- Ubuntu下Mac主题的安装与配置(上篇)
- Android 实现用户列表信息滑动删除功能和选择删除功能
- GridView动态创建列、多行表头