zoukankan      html  css  js  c++  java
  • Hibernate多对多关联

    多对多关联:

    示例:Teacher和Student,一个Teacher可以教很多student,一个Student也可以被很多teacher教

    多对多单向关联

    Teacher知道自己教了哪些学生,Student不知道教自己的有哪些老师

    在Teacher中建(Set集合形式的)Student对象,并添加@ManyToMany注解

    1.建Teacher实体类和Student实体类,添加Annotation注解,如下

    @Entity
    public class Teacher {
    	private int id;
    	private String name;
    	
    	private Set<Student> students = new HashSet<Student>();
    	
    	@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;
    	}
    	
    	@ManyToMany
    //	@JoinTable(name="t_s",
    //	           joinColumns={@JoinColumn(name="t_id")},
    //	           inverseJoinColumns={@JoinColumn(name="s_id")}
    //	)
    	public Set<Student> getStudents() {
    		return students;
    	}
    	public void setStudents(Set<Student> students) {
    		this.students = students;
    	}
    	
    }

    @Entity
    public class Student {
    	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;
    	}
    }

    2.在hibernate.cfg.xml中添加mapping语句

    <mapping class="com.hibernate.model.Teacher"/>
    <mapping class="com.hibernate.model.Student"/>

    3.建Junit测试类

    public class ORMappingTest {
    	
    	@Test
    	public void test() {	
    		new SchemaExport(new Configuration().configure()).create(true, true);		
    	}
    }

    程序至此结束,运行程序,在数据库中生成表teacher、表student以及中间表teacher_student(默认名)。

    中间表teacher_student中会自动生成属性名为Teacher_id和students_id的两个外键,

    可用@JoinTable注解修改中间表的名字及其属性名,如下:

    @ManyToMany
    @JoinTable(name="t_s",
               joinColumns={@JoinColumn(name="t_id")},
    	    inverseJoinColumns={@JoinColumn(name="s_id")}
    )
    public Set<Student> getStudents() {
    	return students;
    }

    这样会在数据库中生成名为t_s的中间表,表中有属性名为t_id和s_id的两个外键。

    多对多双向关联

    Teacher知道自己教了哪些学生,Student也知道教自己的有哪些老师

    在Teacher中建(Set集合形式的)Student对象,在Student中建(Set集合形式的)Teacher对象,并添加@ManyToMany注解

    1.建Teacher实体类和Student实体类,添加Annotation注解

    Teacher类,同上

    Student类,如下:

    @Entity
    public class Student {
    	private int id;
    	private String name;
    	
    	private Set<Teacher> teachers = new HashSet<Teacher>();
    
    	@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;
    	}
    	
    	@ManyToMany(mappedBy="students")     //多对多关系
    	public Set<Teacher> getTeachers() {
    		return teachers;
    	}
    	public void setTeachers(Set<Teacher> teachers) {
    		this.teachers = teachers;
    	}	
    }

    2.在hibernate.cfg.xml中添加mapping语句----同上

    3.建Junit测试类----同上

    程序至此结束,运行程序,在数据库中生成表teacher、表student以及中间表teacher_student(默认名)。

    中间表teacher_student中会自动生成属性名为Teacher_id和students_id的两个外键,

    可用@JoinTable注解修改中间表的名字及其属性名,

    这样会在数据库中生成名为t_s的中间表,表中有属性名为t_id和s_id的两个外键。

  • 相关阅读:
    杭电1029--Ignatius and the Princess IV(哈希)
    杭电1465--不容易系列之一
    杭电1021--Fibonacci Again
    杭电5018--Revenge of Fibonacci
    UVa10651(记忆化搜索)
    <Win32_5>深入浅出Win32的计时器
    偷个空,写个博客——各种沟通各种纠结
    Arbitrage HDU
    常用的字符串处理方法
    无法捕获的异常:MissingMethodException
  • 原文地址:https://www.cnblogs.com/weilunhui/p/3898054.html
Copyright © 2011-2022 走看看