hibernate多对多关系映射
多个教师对应多个学生
Teacher.java
Student.java
HibernateUtil.java
hibernate.cfg.xml
Teacher.hbm.xml
Student.hbm.xml
Test.java
mysql数据库
多个教师对应多个学生
Teacher.java
package com.aabnn.vo;
import java.util.HashSet;
import java.util.Set;
public class Teacher {
private int id;
private String name;
private Set<Student> students=new HashSet<Student>();
public int getId() {
return id;
}
public Teacher(){
}
public Teacher(String name,Set<Student> students){
this.name=name;
this.students=students;
}
@SuppressWarnings("unused")
private void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudents(){
return students;
}
public void setStudents(Set<Student> students){
this.students=students;
}
}
Student.java
package com.aabnn.vo;
import java.util.HashSet;
import java.util.Set;
public class Student {
private int id;
private String name;
private Set<Teacher> teachers = new HashSet<Teacher>();
public Student(){}
public Student(String name){
this.name=name;
}
public Student(String name,Set<Teacher> teachers){
this.name=name;
this.teachers=teachers;
}
public int getId() {
return id;
}
@SuppressWarnings("unused")
private void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
HibernateUtil.java
package com.aabnn.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sf;
static{
sf=new Configuration().configure().buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sf;
}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--mysql connection -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mytest</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--c3p0 connection pool -->
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<!-- mysql dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/aabnn/vo/Teacher.hbm.xml" />
<mapping resource="com/aabnn/vo/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Teacher.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.aabnn.vo">
<class name="Teacher" table="teacher">
<comment>先映射主键</comment>
<id name="id" column="id">
<generator class="identity" />
</id>
<property name="name" type="string" column="name" />
<set name="students" table="teacherstudent" cascade="save-update">
<key column="teacherid" />
<many-to-many class="Student" column="studentid" />
</set>
</class>
<!--name 的值是Teacher类集合属性,table值是关联表,cascade值是在操作数据库是否级联.-->
<!-- key值是关联表的外键 -->
<!--many-to-many 描述关联表。-->
</hibernate-mapping>
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.aabnn.vo">
<class name="Student" table="student">
<comment>先映射主键</comment>
<id name="id" column="id">
<generator class="identity" />
</id>
<property name="name" type="string" column="name" />
<set name="teachers" table="teacherstudent" inverse="true">
<key column="studentid"/>
<many-to-many class="Teacher" column="teacherid" />
</set>
</class>
</hibernate-mapping>
Test.java
package com.aabnn.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import com.aabnn.util.HibernateUtil;
import com.aabnn.vo.Student;
import com.aabnn.vo.Teacher;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Session sess=HibernateUtil.getSessionFactory().openSession();
Transaction tx=sess.beginTransaction();
try{
Student s1=new Student("xiaojian");
Student s2=new Student("wangpeng");
Student s3=new Student("hongsheng");
Set<Student> students=new HashSet<Student>();
students.add(s1);
students.add(s2);
students.add(s3);
Teacher t1=new Teacher("wangshuang",students);
sess.persist(t1);
tx.commit();
}catch(Exception e){
tx.rollback();
}finally{
HibernateUtil.getSessionFactory().close();
}
}
}
mysql数据库
create database mytest;
create table teacher(
id integer unsigned not null auto_increment,
name varchar(45),
primary key(id));
create table student(
id integer unsigned not null auto_increment,
name varchar(45),
primary key(id));
create table teacher_student(
teacherid integer unsigned not null,
studentid integer unsigned not null,
primary key(teacherid,studentid));