Hibernate Annotation 多对多 双向 示例
来源:互联网 发布:神话崔文子 知乎 编辑:程序博客网 时间:2024/05/16 04:40
学生和老师的关系
学生同时有多个老师,老师同时也有多个学生
Student.java 学生实体类
package com.model;//包名import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToMany;@Entitypublic class Student {private int id;private String name;private Set<Teacher> teachers = new HashSet<Teacher>();//持有对方的存放(关系)对象。类型必须是“Set”接口类型@Transient//数据库中没有的字段,需要在这个字段中加上@Transient注释@ManyToMany(mappedBy="students")// 映射对应类的关系持有对象(Set对象名),双向关系必须设置 “mappedBy”public Set<Teacher> getTeachers() {return teachers;}public void setTeahcers(Set<Teacher> teachers) {this.teachers = teachers;}@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;}}
==============================================================================
Teacher.java 老师实体类
package com.model;//包名import java.util.HashSet;import java.util.Set;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.*;@Entitypublic class Teacher {private int id;private String name;private Set<Student> students = new HashSet<Student>();//持有对方的存放(关系)对象。类型必须是“Set”接口类型@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;}@ManyToMany//两个实体类都为@ManyToMany@JoinTable(//设置中间表name="t_s",//中间表的表名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;}}
=============================================================================
JUnit 测试类
import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.junit.Test;public class T_STest {@Testpublic void testSchemaExport() {new SchemaExport(new AnnotationConfiguration().configure()).create(false,true);}}
==========================================================================
生成SQL语句
create table Student ( id integer not null auto_increment, name varchar(255), primary key (id) ) create table Teacher ( id integer not null auto_increment, name varchar(255), primary key (id) ) create table t_s ( teacher_id integer not null, student_id integer not null, primary key (teacher_id, student_id) ) alter table t_s add index FK1BF6843F43EE8 (teacher_id), add constraint FK1BF6843F43EE8 foreign key (teacher_id) references Teacher (id) alter table t_s add index FK1BF683358F448 (student_id), add constraint FK1BF683358F448 foreign key (student_id) references Student (id)
- Hibernate Annotation 多对多 双向 示例
- Hibernate Annotation 多对一 双向
- Hibernate多对多双向关联(Annotation配置)
- Hibernate多对多双向关联(Annotation配置)
- 【hibernate框架】多对多双向关联(Annotation实现)
- Hibernate多对多双向关联(Annotation配置)
- Hibernate annotation 多对多双向关联(很少用)
- 【hibernate框架】一对多(多对一)双向关联(Annotation实现)
- Hibernate关系映射(七)多对多双向关联@ManyToMany Annotation方式
- 【Hibernate】多对多(双向)
- Hibernate多对多双向关联,Annotation与映射文件方式。解决中间表无法插入数据。
- Hibernate Annotation 多对多 配置
- Hibernate annotation多对多配置
- Hibernate annotation 一对多,多对一
- Hibernate annotation 多对多单向关联
- Hibernate Annotation 多对一单向关联
- Hibernate一对一双向(Annotation)
- Hibernate多对多双向关联映射
- 【JAVA】使用jacob生成的html,关于文字乱码处理,图片无法显示等问题。
- 文件,目录和文本处理命令
- 模型计算机仿真程序设计与实现
- 云计算时代:大数据泡沫正无限膨胀
- 插入图片到数据库(BLOB大字段保存对象)
- Hibernate Annotation 多对多 双向 示例
- 使用ActiveMQ的BLOBMessage
- 分类器评价、混淆矩阵与ROC曲线
- google 的 ITsupport
- 谁在妖魔化网络游戏
- KDD 2011的关于Scaling Up Machine Learning的Tutorial
- java web学习笔记(javascript继承)
- XMLHttpRequest 的介绍
- 虚拟机中在红帽的Linux 下安装yum工具