一对多与多对一的映射关系的数据库表如下:
可以这样的来实现:
package cn.hnpi.lis.entity.ch615t;
import java.util.Set;
public class Classes {
private Integer id;
private String name;
private Set<Student> students;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
public Set<Student> getStudents() {
return students;
}
}
package cn.hnpi.lis.entity.ch615t;
public class Student {
private Integer id;
private String name;
private int age;
private Classes classes;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
}
Classes.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">
<!-- xml中书写提示:Alt+/ -->
<hibernate-mapping package="cn.hnpi.lis.entity.ch615t">
<class name="Classes" table="t_classes">
<id name="id">
<generator class="native" />
</id>
<property name="name"></property>
<!-- inverse="true":一的一段不维护映射关系,交给多的一方去维护 -->
<set name="students" inverse="true">
<key column="classes_id"></key>
<one-to-many class="Student"/>
</set>
</class>
</hibernate-mapping>
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">
<!-- xml中书写提示:Alt+/ -->
<hibernate-mapping package="cn.hnpi.lis.entity.ch615t">
<class name="Student" table="t_student">
<id name="id">
<generator class="native" />
</id>
<property name="age"></property>
<property name="name"></property>
<many-to-one name="classes" class="Classes" column="classes_id"></many-to-one>
</class>
</hibernate-mapping>
Hibernate的映射文件:
<mapping resource="cn/hnpi/lis/entity/ch615t/Student.hbm.xml" />
<mapping resource="cn/hnpi/lis/entity/ch615t/Classes.hbm.xml" />
测试代码:
package cn.hnpi.lis.entity.ch615t;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import cn.hnpi.lis.utils.HibernateUtil;
public class MyTest {
Session session;
Transaction tx ;
@Before
public void init(){
session = HibernateUtil.getSession();
tx = session.beginTransaction();
}
@After
public void close(){
tx.commit();
HibernateUtil.closeSession(session);
}
@Test
public void testSave(){
Student[]stus=new Student[10];
Classes[] classes = new Classes[3];
for(int i=0;i<stus.length;i++){
stus[i] = new Student();
stus[i].setAge(20+i);
stus[i].setName("zhangsan"+i);
}
for(int i=0;i<classes.length;i++){
classes[i] = new Classes();
classes[i].setName("class"+i);
}
stus[0].setClasses(classes[0]);
stus[1].setClasses(classes[0]);
stus[2].setClasses(classes[0]);
stus[3].setClasses(classes[1]);
stus[4].setClasses(classes[1]);
stus[5].setClasses(classes[1]);
stus[6].setClasses(classes[2]);
stus[7].setClasses(classes[2]);
stus[8].setClasses(classes[2]);
stus[9].setClasses(classes[2]);
for(int i=0;i<stus.length;i++){
session.save(stus[i]);
}
for(int i=0;i<classes.length;i++){
session.persist(classes[i]);
}
}
@Test
public void testLoad(){
}
}
测试结果如下: