多个员工属于一个部门,员工可以知道他属于哪个部门,部门不能得到他的员工
员工一端保存了部门的引用,配置文件中用many-to-one配置。
<many-to-one name="deptment" class="com.shizhan.po.Deptment" fetch="select" cascade="save-update">
<column name="DEPTID" precision="10" scale="0" />
</many-to-one>
cascade属性的值设置为save-update表示存储员工的时候,如果他的部门不存在或者更新了,级联保存或者更新。
部门类
public class Deptment implements java.io.Serializable {
private Long deptid;
private String deptname;
public Long getDeptid() {
return this.deptid;
}
public void setDeptid(Long deptid) {
this.deptid = deptid;
}
public String getDeptname() {
return this.deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
}
员工类:
package com.shizhan.po;
public class Employee implements java.io.Serializable {
private Long epno;
private Deptment deptment;
private String epname;
public Long getEpno() {
return this.epno;
}
public void setEpno(Long epno) {
this.epno = epno;
}
public Deptment getDeptment() {
return this.deptment;
}
public void setDeptment(Deptment deptment) {
this.deptment = deptment;
}
public String getEpname() {
return this.epname;
}
public void setEpname(String epname) {
this.epname = epname;
}
}
部门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>
<class name="com.shizhan.po.Deptment" table="DEPTMENT" schema="SCOTT">
<id name="deptid" type="java.lang.Long">
<column name="DEPTID" precision="10" scale="0" />
<generator class="increment" />
</id>
<property name="deptname" type="java.lang.String">
<column name="DEPTNAME" length="20" />
</property>
</class>
</hibernate-mapping>
员工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>
<class name="com.shizhan.po.Employee" table="EMPLOYEE" schema="SCOTT">
<id name="epno" type="java.lang.Long">
<column name="EPNO" precision="10" scale="0" />
<generator class="increment" />
</id>
<many-to-one name="deptment" class="com.shizhan.po.Deptment" fetch="select" cascade="save-update">
<column name="DEPTID" precision="10" scale="0" />
</many-to-one>
<property name="epname" type="java.lang.String">
<column name="EPNAME" length="20" />
</property>
</class>
</hibernate-mapping>