一、多对一
学生Student和班级Grade实现多对一,多个学生对应一个班级。
Student.java实体类,映射了班级的属性。
package com.lxit.entity;
import java.io.Serializable;
public class Student implements Serializable {
public Student() {
}
public Student(String sname, String sex, String address) {
this.sname = sname;
this.sex = sex;
this.address = address;
}
public Student(String sname, String sex, String address, Grade grade) {
this.sname = sname;
this.sex = sex;
this.address = address;
this.grade = grade;
}
private int sid;
private String sname;
private String sex;
private String address;
//单向多对一:在多的一方封装一的一方的实体对象
private 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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", sex=" + sex
+ ", address=" + address + "]";
}
}
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.lxit.entity.Student" table="Student">
<id name="sid" column="sid">
<generator class="native"></generator>
</id>
<property name="sname" column="sname" type="java.lang.String"></property>
<property name="sex" column="sex"></property>
<property name="address" column="address"></property>
<!-- 实现多对一的映射关系 -->
<many-to-one name="grade" class="com.lxit.entity.Grade" column="gid" lazy="false"></many-to-one>
</class>
</hibernate-mapping>
二、一对多
Grade.java实体类,映射了班级和学生的一对多,包含了学生的集合。
package com.lxit.entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class Grade implements Serializable{
public Grade() {
}
public Grade(String gname) {
this.gname = gname;
}
//在一方添加多方的集合
public Set<Student> students = new HashSet<Student>();
private int gid;
private String gname;
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 Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
Grade映射文件
<?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.lxit.entity.Grade" table="Grade">
<id name="gid" column="gid">
<generator class="native"></generator>
</id>
<property name="gname" type="java.lang.String">
<column name="gname" length="20"></column>
</property>
<!--
inverse:表示将维护外键的职责交友多方来维护,默认为false
cascade:表示级联效果,当保存班级时,班级对应的学生一起保存
-->
<set name="students" table="Student" cascade="all">
<key column="gid"></key>
<one-to-many class="com.lxit.entity.Student"/>
</set>
</class>
</hibernate-mapping>