- packagecom.entity;
- importjava.io.Serializable;
- importjava.util.HashSet;
- importjava.util.Set;
- importjavax.persistence.Column;
- importjavax.persistence.Entity;
- importjavax.persistence.GeneratedValue;
- importjavax.persistence.Id;
- importjavax.persistence.ManyToMany;
- importjavax.persistence.Table;
- @Entity
- @Table(name="Student")
- publicclassStudentimplementsSerializable{
- privateIntegerstudentid;//学生ID
- privateStringstudentName;//学生姓名
- privateSet<Teacher>teachers=newHashSet<Teacher>();//对应的教师集合
- publicStudent(){
- }
- publicStudent(StringstudentName){
- this.studentName=studentName;
- }
- @Id
- @GeneratedValue
- publicIntegergetStudentid(){
- returnstudentid;
- }
- publicvoidsetStudentid(Integerstudentid){
- this.studentid=studentid;
- }
- @Column(nullable=false,length=32)
- publicStringgetStudentName(){
- returnstudentName;
- }
- publicvoidsetStudentName(StringstudentName){
- this.studentName=studentName;
- }
- /*
- *@ManyToMany注释表示Student是多对多关系的一边,mappedBy属性定义了Student为双向关系的维护端
- */
- @ManyToMany(mappedBy="students")
- publicSet<Teacher>getTeachers(){
- returnteachers;
- }
- publicvoidsetTeachers(Set<Teacher>teachers){
- this.teachers=teachers;
- }
- }
Teacher.java
- packagecom.entity;
- importjava.io.Serializable;
- importjava.util.HashSet;
- importjava.util.Set;
- importjavax.persistence.CascadeType;
- importjavax.persistence.Column;
- importjavax.persistence.Entity;
- importjavax.persistence.FetchType;
- importjavax.persistence.GeneratedValue;
- importjavax.persistence.Id;
- importjavax.persistence.JoinTable;
- importjavax.persistence.ManyToMany;
- importjavax.persistence.Table;
- importjavax.persistence.JoinColumn;
- @Entity
- @Table
- publicclassTeacherimplementsSerializable{
- privateIntegerteacherid;//教师ID
- privateStringteacherName;//教师姓名
- privateSet<Student>students=newHashSet<Student>();//对应的学生集合
- publicTeacher(){
- }
- publicTeacher(StringteacherName){
- this.teacherName=teacherName;
- }
- @Id
- @GeneratedValue
- publicIntegergetTeacherid(){
- returnteacherid;
- }
- publicvoidsetTeacherid(Integerteacherid){
- this.teacherid=teacherid;
- }
- @Column(nullable=false,length=32)
- publicStringgetTeacherName(){
- returnteacherName;
- }
- publicvoidsetTeacherName(StringteacherName){
- this.teacherName=teacherName;
- }
- /*
- *@ManyToMany注释表示Teacher是多对多关系的一端。
- *@JoinTable描述了多对多关系的数据表关系,name属性指定中间表名称。
- *joinColumns定义中间表与Teacher表的外键关系,中间表Teacher_Student的Teacher_ID列是Teacher表的主键列对应的外键列。
- *inverseJoinColumns属性定义了中间表与另外一端(Student)的外键关系。
- */
- @ManyToMany(cascade=CascadeType.PERSIST,fetch=FetchType.LAZY)
- @JoinTable(name="Teacher_Student",
- joinColumns={@JoinColumn(name="teacher_ID",referencedColumnName="teacherid")},
- inverseJoinColumns={@JoinColumn(name="student_ID",referencedColumnName="studentid")
- })
- publicSet<Student>getStudents(){
- returnstudents;
- }
- publicvoidsetStudents(Set<Student>students){
- this.students=students;
- }
- publicvoidaddStudent(Studentstudent){
- if(!this.students.contains(student)){//检测在该散列表中某些键是否映射到指定值,value查找的值。如果某些键映射到该散列表中的值为true,否则false
- this.students.add(student);
- }
- }
- publicvoidremoveStudent(Studentstudent){
- this.students.remove(student);
- }
- }
转自:http://lym6520.iteye.com/blog/314065
相关推荐
hibernate中manytomany关系 实体类中有个集合方的是这个实体类本身
我如何学习重要的TypeORM技巧(排序关系数组,按关系数组过滤) 本文至少需要有关typeorm的基础知识。 本教程的初始状态是一个简单的user实体,如下所示: @Entity( " users " )export default class User extends ...
为了实现多对多的关系,数据库中需要关联表,用以在两个实体间建立关联。JBoss可以自动生成关联表,你也可以@AssociationTable来指定关联表的信息。 如: @ManyToMany(cascade = {CascadeType.CREATE, CascadeType....
所有者是Book类,我们没有在所有者类中放置mappedBy (在这里我们犯了一个错误)作者实体类与book类相关-在这里,它必须是mappedBy并指向另一侧的集合,可以正常工作。 请测试代码并查看测试,我建议您@JoinColumn...
用于映射多对多关系的 Hibernate 注释
通过关联表来保存两个实体之间的连接关系(要模拟一对一关联必须在每一个外键上添加唯一约束)。 1.共享主键的一对一关联映射: @Entity @Table(name="Test_Body") public class Body { private Integer id; ...
Hibernate注释大全收藏 声明实体Bean @Entity public class Flight implements Serializable { Long id; @Id public Long getId() { return id;...映射实体Bean的关联关系 一对一 使用 @OneToOne...
问题:如何跨多对多关系的两个实体投影列示例:员工(id、名字、姓氏)会议(id、主题、会议日期) 假设我们有员工数据 1 , A, AA 2 , B, BB 会议 1,AB 会议,X 2,另一个会议,X 3,仅 B Employee_Meeting AB A 和...
实体之间的关系策略是MANYtoMANY-可以将多个客户分配到一个帐户,可以将多个帐户分配给一个客户。 只有Customer对象保留了该关系的引用-即使MongoDB没有明确的级联,关系也正在级联功能性Project正在公开一个简单的...
实体将City,Tag,Comment,Country和Year类设置为具有OneToMany,ManyToOne和ManyToMany关系的实体。 储存库实例五个存储库接口扩展了CRUD存储库。 CityRepository还包含findByCountry,findByCountryId,...
Mp12-社交网络(Fakebook)(人)[ManyToMany] 该应用程序将具有一个名为Person的实体,可以通过/ persons端点进行管理。 帖子将添加一个人,获取将显示所有这些人。 为了使一个人与另一个人成为朋友,我们将向终端...
ManyToMany等关系。 让您非常快速地编写数据库使用者程序。 奥尔曼是... 开源(根据 Apache License 2.0 许可) 轻量级和小 (~170 kb)(替代品如 Hibernate ~4 mb) 易于安装 易于使用注释进行配置,不需要 XML 配置...