话不多说,直接上代码示例
1、父表的POJO类InternationalClass.java
package com.international.model;
import java.util.Set;
public class InternationalClass {
private int classId;
private String className;
private String grade;
private String major;
private String reserves1;
private String reserves2;
private String reserves3;
private Set<InternationalStudent> interStudents; //关联的对象
public Set<InternationalStudent> getInterStudents() {
return interStudents;
}
public void setInterStudents(Set<InternationalStudent> interStudents) {
this.interStudents = interStudents;
}
public int getClassId() {
return classId;
}
public void setClassId(int classId) {
this.classId = classId;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getReserves1() {
return reserves1;
}
public void setReserves1(String reserves1) {
this.reserves1 = reserves1;
}
public String getReserves2() {
return reserves2;
}
public void setReserves2(String reserves2) {
this.reserves2 = reserves2;
}
public String getReserves3() {
return reserves3;
}
public void setReserves3(String reserves3) {
this.reserves3 = reserves3;
}
}
2、父表的映射文件InternationalClass.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.international.model.InternationalClass" table="InternationalClass">
<id name="classId" column="classId">
<!-- 自增主键策略 -->
<generator class="native"></generator>
</id>
<property name="className" column="className"></property>
<property name="grade" column="grade"></property>
<property name="major" column="major"></property>
<property name="reserves1" column="reserves1"></property>
<property name="reserves2" column="reserves2"></property>
<property name="reserves3" column="reserves3"></property>
<!-- 关联的国际班学生对象 -->
<set name="interStudents" cascade="all" table="internationalStudent" inverse="true">
<key column="classId" not-null="true"></key>
<one-to-many class="com.international.model.InternationalStudent"/>
</set>
</class>
</hibernate-mapping>
3、子表的POJO类InternationalStudent.java
package com.international.model;
public class InternationalStudent {
private String studentId;
private String studentName;
private String sex;
private String password;
private String status;
private String reserves1;
private String reserves2;
private String reserves3;
private InternationalClass classes; //定义InternationalClass类的classes对象
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getReserves1() {
return reserves1;
}
public void setReserves1(String reserves1) {
this.reserves1 = reserves1;
}
public String getReserves2() {
return reserves2;
}
public void setReserves2(String reserves2) {
this.reserves2 = reserves2;
}
public String getReserves3() {
return reserves3;
}
public void setReserves3(String reserves3) {
this.reserves3 = reserves3;
}
public InternationalClass getClasses() {
return classes;
}
public void setClasses(InternationalClass classes) {
this.classes = classes;
}
}
4、子表的映射文件InterantionalStudent.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.international.model.InternationalStudent" table="InternationalStudent">
<id name="studentId" column="studentId">
<!-- 非自增主键生成策略 -->
<generator class="assigned"></generator>
</id>
<property name="studentName" column="studentName"></property>
<property name="sex" column="sex"></property>
<property name="password" column="password"></property>
<property name="status" column="status"></property>
<!-- <property name="classId" column="classId"></property> -->
<!-- 双向n->1关联,外键,由InternationalStudent指向InternationalClass -->
<many-to-one name="classes" class="com.international.model.InternationalClass"
lazy="false" fetch="join" column="classId" />
</class>
</hibernate-mapping>
分析,上面就是两张表关联起来的映射文件和POJO类了,没贴业务代码。注意里面起关联的部分,还有
cascade=“all”
这个属性,我曾在父子表都设置过,然后子表执行删除操作的时候,悲剧来了,级联操作把父表对应的班级信息给我删了。。。所以导致子表的学生信息也对应没了。所以要想做到删除学生信息不影响班级信息,删除班级信息会影响学生信息,此时在子表的cascade级联属性就得设置成cascade="none"
或者不写默认就是none。
网友评论