继上回讲到 Hibernate中的单向一对多关联 ,这次来实现一下Hibernate中的单向多对一关联。对原来的项目修改如下:
Hibernate中的单向多对一关联,需要我们在多方增加一个一方的属性,并且在多方的映射配置文件中添加many-to-one的配置。
Grade.java
package com.imooc.entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/**
* Created by DreamBoy on 2016/5/18.
*/
public class Grade implements Serializable {
private int gid;
private String gname;
private String gdesc;
public Grade() {
}
public Grade(int gid, String gname, String gdesc, Set<Student> students) {
this.gid = gid;
this.gname = gname;
this.gdesc = gdesc;
}
public Grade(String gname, String gdesc) {
this.gname = gname;
this.gdesc = 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;
}
}
Grade.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">
<hibernate-mapping>
<class name="com.imooc.entity.Grade" table="grade">
<id name="gid" column="gid" type="java.lang.Integer">
<generator class="increment"/>
</id>
<property name="gname" type="java.lang.String">
<column name="gname" length="20" not-null="true"/>
</property>
<property name="gdesc">
<column name="gdesc"/>
</property>
</class>
</hibernate-mapping>
Student.java
package com.imooc.entity;
import java.io.Serializable;
/**
* Created by DreamBoy on 2016/5/18.
*/
public class Student implements Serializable {
private int sid;
private String sname;
private String sex;
//在多方定义一个一方的引用
private Grade grade;
public Student() {
}
public Student(String sname, String sex) {
this.sname = sname;
this.sex = 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 Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
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">
<hibernate-mapping>
<class name="com.imooc.entity.Student" table="student">
<id name="sid" column="sid" type="java.lang.Integer">
<generator class="increment"/>
</id>
<property name="sname" type="java.lang.String">
<column name="sname" length="20" not-null="true"/>
</property>
<property name="sex">
<column name="sex"/>
</property>
<!-- 配置多对一关联关系 -->
<many-to-one name="grade" class="com.imooc.entity.Grade" column="gid"></many-to-one>
</class>
</hibernate-mapping>
重要的配置为:
<!-- 配置多对一关联关系 -->
<many-to-one name="grade" class="com.imooc.entity.Grade" column="gid"></many-to-one>
Test.java
package com.imooc.test;
/**
* Created by DreamBoy on 2016/5/18.
*/
import com.imooc.entity.Grade;
import com.imooc.entity.Student;
import com.imooc.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.Set;
/**
* 单向多对一(学生---》班级)
*/
public class Test {
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("哇", "男");
//设置关联关系,指定多到一的关联关系
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);
}
}
注意:在执行Test类前,需要删除掉上次项目中数据库中表的数据。HibernateUtil类与上回项目中的一致,Hibernate的配置文件也一致。
总结: