欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate--多对多单向(双向)关联

程序员文章站 2022-04-22 16:11:15
...

Teacher和Student来说明:

------------------------------------------------------------

Annotation注解:

teacher->student

Teacher.java:

package com.bean;

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.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

@Entity
public class Teacher {
	private int id;
	private String name;
	private Set<Student> students = new HashSet<Student>();

	@ManyToMany
	@JoinTable(name="t_s",
              [email protected](name="TeacherID"),
              [email protected](name="stuendID")
			)
	public Set<Student> getStudents() {
		return students;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}

	@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;
	}

}


Student.java:

package com.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@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;
	}

}

cfg.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>    
     <property name="connection.url">jdbc:mysql://localhost:3306/user</property>     
     <property name="connection.username">root</property>   
     <property name="connection.password">root</property>
     
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
     <property name="show_sql">true</property>
     
     <property name="hbm2ddl.auto">update</property>
     <property name="show_sql">true</property>
     <property name="format_sql">true</property>  
     
     <property name="current_session_context_class">thread</property>
       <mapping class="com.bean.Student"/>
     <mapping class="com.bean.Teacher"/>  
   </session-factory>
</hibernate-configuration>
        


XML配置:

Student.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name = "com.bean.Student" table = "student" >

     <id name = "id" column="id">
       <generator class="native"/>
     </id>
     <property name="name" />
  </class>
</hibernate-mapping>        
 
Teacher.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name = "com.bean.Teacher" table = "teacher" >

     <id name = "id" column="id">
       <generator class="native"/>
     </id>
     <property name="name" />
     <set name="students" table="t_s">
       <key column="teacher_Id"></key>
       <many-to-many class="com.bean.Student" column="student_Id"/>
     </set>
  </class>
</hibernate-mapping>        
 
cfg.xml中修改映射配置
   <mapping resource="com/bean/Student.hbm.xml"/>
    <mapping resource="com/bean/Teacher.hbm.xml"/>


------------------------------------------------------------------------------------------

多对多双向关联:

1、xml配置:
Student.hbm.xml中添加配置即可:

       <set name="teachers" table="t_s">
       <key column="student_Id"></key>
       <many-to-many class="com.bean.Teacher" column="teacher_Id"/>
     </set>


2、Annotation配置:

在Student.java中添加:

	@ManyToMany(mappedBy="students")
	public Set<Teacher> getTeachers() {
		return teachers;
	}

	public void setTeachers(Set<Teacher> teachers) {
		this.teachers = teachers;
	}