所谓一对多映射
在数据库中我们通常会通过添加外键的方式将表关联起来,表现一对多的关系。
而在Hibernate中,我们则要通过在一方持有多方的集合来实现,即在“一”的一端中使用元素表示持有“多”的一段的对象。
下面是针对这个知识点的一个小案例:
Grade.java:
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class Grade implements Serializable {
private int gid;
private String gname;
private String gdesc;
private Set<Student> students;
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public String getGdesc() {
return gdesc;
}
public void setGdesc(String gdesc) {
this.gdesc = gdesc;
}
public Grade() {
super();
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
public Grade(int gid, String gname, String gdesc) {
super();
this.gid = gid;
this.gname = gname;
this.gdesc = gdesc;
}
public Grade(String gname, String gdesc) {
super();
this.gname = gname;
this.gdesc = gdesc;
}
}
Student.java:
import java.io.Serializable;
public class Student implements Serializable {
private int sid;
private String sname;
private String sex;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Student() {
super();
}
public Student(String sname, String sex) {
super();
this.sname = sname;
this.sex = sex;
}
}
然后是hbm.xml文件的配置:
Grade.hbm.xml:
<?xml version="1.0"?>
<!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.imooc.entity.Grade" table="grade">
<id name="gid" column="gid" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="gname" type="java.lang.String">
<column name="gname" length="20" not-null="true"></column>
</property>
<property name="gdesc">
<column name="gdesc"></column>
</property>
<!-- 配置一对多关联关系 -->
<set name="students" table="student">
<key column="gid"></key>
<one-to-many class="com.imooc.entity.Student"/>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml:
<?xml version="1.0"?>
<!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.imooc.entity.Student" table="student">
<id name="sid" column="sid" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="sname" type="java.lang.String">
<column name="sname" length="20" not-null="true"></column>
</property>
<property name="sex">
<column name="sex"></column>
</property>
</class>
</hibernate-mapping>
最后,在hibernate.cfg.xml文件中将映射声明一下即可:
<mapping resource="com./xx/Grade.hbm.xml" />
<mapping resource="com/XX/Student.hbm.xml" />
代码实现操作的结果:
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.imooc.entity.Grade;
import com.imooc.entity.Student;
import com.imooc.util.HibernateUtil;
/*
* 单向一对多关系关系(班级--->学生)
* 建立关联关系后,可以方便的从一个对象导航到另一个对象
* 注意关联的方向
*/
public class Test {
public static void main(String[] args) {
//add();
//findStudentsByGrade();
//update();
delete();
}
//将学生添加到班级
public static void add(){
Grade g=new Grade("Java一班", "Java软件开发一班");
Student stu1=new Student("张三", "男");
Student stu2=new Student("穆女神", "女");
//如果希望在学生表中添加对应的班级编号,需要在班级中添加学生,建立关联关系
/*g.getStudents().add(stu1);
g.getStudents().add(stu2);*/
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
session.save(g);
session.save(stu1);
session.save(stu2);
tx.commit();
HibernateUtil.closeSession(session);
}
//查询班级中包含的学生
public static void findStudentsByGrade(){
Session session=HibernateUtil.getSession();
Grade grade=(Grade) session.get(Grade.class, 1);
System.out.println(grade.getGname()+","+grade.getGdesc());
/*Set<Student> students=grade.getStudents();
for(Student stu:students){
System.out.println(stu.getSname()+","+stu.getSex());
}*/
}
//修改学生信息
public static void update(){
Grade g=new Grade("Java二班", "Java软件开发二班");
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
Student stu=(Student) session.get(Student.class, 1);
//g.getStudents().add(stu);
session.save(g);
tx.commit();
HibernateUtil.closeSession(session);
}
//删除学生信息
public static void delete(){
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
Student stu=(Student) session.get(Student.class, 2);
session.delete(stu);
tx.commit();
HibernateUtil.closeSession(session);
}
}
所谓单向多对一映射
多对一的关系和关系型数据库的外键参照关系最为匹配,即在乙方的表中的一个外键参照另一个表中的主键。 而在Hibernate中就可以通过
在多方持有乙方的引用来实现,需要在“多”的一段使用标签来进行设置。
下面是针对这个知识点的一个小案例:
Grade.java:
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class Grade implements Serializable {
private int gid;
private String gname;
private String gdesc;
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public String getGdesc() {
return gdesc;
}
public void setGdesc(String gdesc) {
this.gdesc = gdesc;
}
public Grade() {
super();
}
public Grade(int gid, String gname, String gdesc) {
super();
this.gid = gid;
this.gname = gname;
this.gdesc = gdesc;
}
public Grade(String gname, String gdesc) {
super();
this.gname = gname;
this.gdesc = gdesc;
}
}
Student.java:
import java.io.Serializable;
public class Student implements Serializable {
private int sid;
private String sname;
private String sex;
// 在多方定义一个一方的引用
private Grade grade;
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Student() {
super();
}
public Student(String sname, String sex) {
super();
this.sname = sname;
this.sex = sex;
}
}
然后是hbm.xml文件的配置:
Grade.hbm.xml:
<?xml version="1.0"?>
<!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.imooc.entity.Grade" table="grade">
<id name="gid" column="gid" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="gname" type="java.lang.String">
<column name="gname" length="20" not-null="true"></column>
</property>
<property name="gdesc">
<column name="gdesc"></column>
</property>
</class>
</hibernate-mapping>
Student.hbm.xml:
<?xml version="1.0"?>
<!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.imooc.entity.Student" table="student">
<id name="sid" column="sid" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="sname" type="java.lang.String">
<column name="sname" length="20" not-null="true"></column>
</property>
<property name="sex">
<column name="sex"></column>
</property>
<!-- 配置多对一关联关系 -->
<many-to-one name="grade"
class="com.imooc.entity.Grade" column="gid" cascade="all"></many-to-one>
</class>
</hibernate-mapping>
最后,在hibernate.cfg.xml文件中将映射声明一下即可:
<mapping resource="com./xx/Grade.hbm.xml" />
<mapping resource="com/XX/Student.hbm.xml" />
再来使用代码测试一下:
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.imooc.entity.Grade;
import com.imooc.entity.Student;
import com.imooc.util.HibernateUtil;
/*
* 单向多对一(学生--->班级)
*/
public class Test02 {
public static void main(String[] args) {
save();
}
//保存
public static void save(){
Grade g=new Grade("Java一班","Java软件开发一班");
Student stu1=new Student("慕女神", "女");
Student stu2=new Student("小慕慕", "男");
//设置关联关系
g.getStudents().add(stu1);
g.getStudents().add(stu2);
stu1.setGrade(g);
stu2.setGrade(g);
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
session.save(g);
session.save(stu1);
session.save(stu2);
tx.commit();
HibernateUtil.closeSession(session);
}
}
双向的多对一关系
只需要分别在其.hbm.xml文件中进行相关的声明就可以了。
Grade.hbm.xml文件中添加:
<!-- 配置一对多关联关系 -->
<set name="students" table="student">
<key column="gid"></key>
<one-to-many class="com.imooc.entity.Student"/>
</set>
Student.hbm.xml文件中添加:
<!-- 配置多对一关联关系 -->
<many-to-one name="grade" class="com.imooc.entity.Grade"
column="gid" cascade="all"></many-to-one>
由此,便可完成多对一关系的映射了!