单向多对一

            多个员工属于一个部门,员工可以知道他属于哪个部门,部门不能得到他的员工

     员工一端保存了部门的引用,配置文件中用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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值