hibernate多对多关联关系映射
多个teacher对应多个student
由于多对多关系必须借助关联表来表达关联关系,所以多对多关系只有两种情况:一是带关联表的双向多对对映射,另一种是带关联表的单向多对多映射。
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;
public class Student {
private int id;
private String name;
public Student(){}
public Student(String name){
this.name=name;
}
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;
}
}
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;
}
}
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();
}
}
}
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" />
</class>
</hibernate-mapping>
mysql archive
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));
分享到:
相关推荐
hibernate单向一对多关联映射(注解版)
hibernate单向多对多映射(XML版)
包含《多对多双向关联映射》《多对一单向关联映射》《多对一双向关联映射》《一对多单向关联映射》等文档,并有图解及例子,非常适合新手学习,尤其是刚刚接触hibernate,对映射关系不清楚的。。。。
Hibernate 多对一外键单向关联 Hibernate 多对一连接表单向关联 Hibernate 多对多单向关联 Hibernate 一对一外键双向关联 Hibernate 一对一主键双向关联 Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向...
这里包含了hibernate多对一单向关联关系实现源码,希望对你有用。
NULL 博文链接:https://1028826685.iteye.com/blog/1536060
Hibernate双向一对一关联映射(注解版)
hibernate单向多对多映射(注解版)
hibernate双向多对多关联映射(注解版)
Hibernate学习:单向多对一关联 工程
Hibernate
NULL 博文链接:https://lijunabc.iteye.com/blog/438168
hibernate双向多对多关联映射(XML)
博文链接:https://shaqiang32.iteye.com/blog/201311
hibernate双向一对多关联映射(注解版)
博文链接:https://shaqiang32.iteye.com/blog/201308
hibernate关联映射注解多对多单向关联、
hibernate表之间3种双向关联映射
该资源包含了一些常用的hibernate关联映射实例。包括一对多,多对多,继承映射,等,是学习hibernate的必备品