这次的笔记是hibernate 的一对多 关系,常见的如一个班级多个学生,和多对一是相反的角度来讲的,不过代码还是有些差别,上代码了。
一对多单向:
Classroom.java
package com.ywj.TestHibernate.e;
import java.io.Serializable;
import java.util.Set;
public class Classroom implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5730317497296467931L;
private Long id;
private String name;
private Set<Student> student;// 多个学生
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = student;
}
}
Classroom.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ywj.TestHibernate.e">
<class name="Classroom" table="classroom">
<!-- 生成对象唯一的OID标示符 -->
<id name="id" column="id" type="java.lang.Long">
<generator class="identity" />
</id>
<!-- 普通写法 -->
<property name="name" />
<!-- 一对多的写法 -->
<set name="student" ><!-- inverse="true" -->
<key column="classroom"></key><!-- 这个column会在student表中生成 -->
<one-to-many class="Student"/>
</set>
</class>
</hibernate-mapping>
Student.java
package com.ywj.TestHibernate.e;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 9053757358736102041L;
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ywj.TestHibernate.e">
<class name="Student" table="student">
<!-- 生成对象唯一的标示符 -->
<id name="id" column="id" type="java.lang.Long">
<generator class="identity" />
</id>
<!-- 普通写法 -->
<property name="name" />
</class>
</hibernate-mapping>
Test.java
package com.ywj.TestHibernate.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.ywj.TestHibernate.e.Classroom;
import com.ywj.TestHibernate.e.Student;
public class Test {
public static void main(String[] args) {
Session s = HibernateUtils.getCurrentSession();
Transaction t = null;
try {
t = s.beginTransaction();
// 添加
Classroom classroom = new Classroom();
classroom.setName("classroom");
Student st1 = new Student();
st1.setName("stu1");
Student st2 = new Student();
st2.setName("stu2");
Set<Student> set = new HashSet<Student>();
set.add(st2);
set.add(st1);
classroom.setStudent(set);
s.save(classroom);
s.save(st1);
s.save(st2);
// 多出2条update sql,这多出的sql主要是更新student表 让student的classroom补上相关的值
// 双向关联可以避免这个多出update sql问题
t.commit();
// 查询
Classroom cls = (Classroom) s.load(Classroom.class, 1L);
System.out.println(cls.getStudent().size());
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
s.close();
}
}
}
Classroom.java
package com.ywj.TestHibernate.e;
import java.io.Serializable;
import java.util.Set;
public class Classroom implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5730317497296467931L;
private Long id;
private String name;
private Set<Student> student;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = student;
}
}
Classroom.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ywj.TestHibernate.e">
<class name="Classroom" table="classroom">
<!-- 生成对象唯一的OID标示符 -->
<id name="id" column="id" type="java.lang.Long">
<generator class="identity" />
</id>
<!-- 普通写法 -->
<property name="name" />
<!-- 一对多的写法 -->
<set name="student" inverse="true">
<!-- inverse="true" 的作用是 classroom不用记住他的student是谁,也就是不用classroom来维护关系 ,由student来维护
比如 学校校长不必记录我校的学生是谁,只要学生记住我这个校长是谁就行了 -->
<key column="classroom"></key><!-- 这个column要和student中的<many-to-one>中的column一样 -->
<one-to-many class="Student"/>
</set>
</class>
</hibernate-mapping>
Student.java
package com.ywj.TestHibernate.e;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 9053757358736102041L;
private Long id;
private String name;
private Classroom classroom;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Classroom getClassroom() {
return classroom;
}
public void setClassroom(Classroom classroom) {
this.classroom = classroom;
}
}
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ywj.TestHibernate.e">
<class name="Student" table="student">
<!-- 生成对象唯一的标示符 -->
<id name="id" column="id" type="java.lang.Long">
<generator class="identity" />
</id>
<!-- 普通写法 -->
<property name="name" />
<!-- 生成一个外键classroom,对应classroom表的id -->
<many-to-one name="classroom" column="classroom" class="Classroom"></many-to-one>
</class>
</hibernate-mapping>
Test.java
package com.ywj.TestHibernate.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.ywj.TestHibernate.e.Classroom;
import com.ywj.TestHibernate.e.Student;
public class Test {
public static void main(String[] args) {
Session s = HibernateUtils.getCurrentSession();
Transaction t = null;
try {
t = s.beginTransaction();
// 添加
Classroom classroom = new Classroom();
classroom.setName("classroom");
Student st1 = new Student();
st1.setName("stu1");
st1.setClassroom(classroom);
Student st2 = new Student();
st2.setName("stu2");
st2.setClassroom(classroom);
/*
不用classroon来维护关系了,由student来维护关系就行了,在classroom.hbm.xml中配置了inverse="true"
Set<Student> set = new HashSet<Student>();
set.add(st2);
set.add(st1);
classroom.setStudent(set);
*/
s.save(classroom);
s.save(st1);
s.save(st2);
// 这里没有多出update语句啦,因为不用classroom来维护关系啦
t.commit();
// 查询
/*Classroom cls = (Classroom) s.load(Classroom.class, 1L);
System.out.println(cls.getStudent().size());
Student stu = (Student) s.load(Student.class, 1L);
System.out.println(stu.getClassroom().getName());*/
} catch (Exception e) {
e.printStackTrace();
t.rollback();
} finally {
s.close();
}
}
}
OK.......