第十章 基于Annotation的关系映射 多对多
2013-06-09 12:01
295 查看
如果下面部分内容有不明白的可以查找:
基于Annotation的关系映射 前期准备:http://blog.csdn.net/p_3er/article/details/9061911
基于xml的多对多:http://blog.csdn.net/p_3er/article/details/9053415
Student:
Teacher:
基于Annotation的关系映射 前期准备:http://blog.csdn.net/p_3er/article/details/9061911
基于xml的多对多:http://blog.csdn.net/p_3er/article/details/9053415
Student:
@Entity @Table(name = "student", catalog = "hibernate") public class Student implements java.io.Serializable { private Integer id; private String SName; private Set<Teacher> teachers; public Student() { } @Id @GeneratedValue @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "s_name", nullable = false, length = 45) public String getSName() { return this.SName; } public void setSName(String SName) { this.SName = SName; } @ManyToMany(cascade=CascadeType.ALL,targetEntity=cn.framelife.hibernate.entity.Teacher.class) @JoinTable(name="s_t",catalog="hibernate", joinColumns={@JoinColumn(name="student_id")}, inverseJoinColumns={@JoinColumn(name="teacher_id")} ) /* 映射文件方式比较: <set name="teachers" table="s_t" cascade="all"> <key column="student_id"/> <many-to-many column="teacher_id" class="cn.framelife.hibernate.entity.Teacher"></many-to-many> </set> */ public Set<Teacher> getTeachers() { return teachers; } public void setTeachers(Set<Teacher> teachers) { this.teachers = teachers; } }
Teacher:
@Entity @Table(name = "teacher", catalog = "hibernate") public class Teacher implements java.io.Serializable { private Integer id; private String TName; private Set<Student> students; public Teacher() { } @Id @GeneratedValue @Column(name = "id", unique = true, nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "t_name", nullable = false, length = 45) public String getTName() { return this.TName; } public void setTName(String TName) { this.TName = TName; } @ManyToMany(cascade=CascadeType.ALL,targetEntity=cn.framelife.hibernate.entity.Student.class) @JoinTable(name="s_t",catalog="hibernate", joinColumns={@JoinColumn(name="teacher_id")}, inverseJoinColumns={@JoinColumn(name="student_id")} ) public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }
相关文章推荐
- 第十章 基于Annotation的关系映射 多对多
- 第十章 基于Annotation的关系映射 多对一与一对多
- 第十章 基于Annotation的关系映射 一对一
- 第十章 基于Annotation的关系映射 前期准备
- 第十章 基于Annotation的关系映射 一对一
- 第十章 基于Annotation的关系映射 多对一与一对多
- Hibernate中,One2Many和Many2One的双向映射关系的基于注解Annotation实现方式
- Hibernate -- 注解(Annotation)关系映射
- mybatis整合spring 之 基于接口映射的多对一关系
- Hibernate -- 注解(Annotation)关系映射
- hibernate annotation注解方式来处理映射关系
- hibernate annotation注解方式来处理映射关系
- Hibernate 关系映射(四) 基于中间表关联的单向1:1
- Hibernate关系映射(二) 基于外键的双向一对一
- hibernate关系映射小结 - annotation
- MyBatis框架基于Annotation注解的一对多关联映射
- Hibernate关系映射(二)一对一双向外键关联@OneToOne Annotation方式
- hibernate annotation注解方式来处理映射关系
- mybatis整合spring 之 基于接口映射的多对一关系
- Hibernate 关系映射(三) 基于外键关联的单向 1:1