Hibernate 继承映射到表

   最近在学习Hibernate的继承映射到表,下面就以我做过的例子来讲一下Hibernate如何实现把类和关系的各种关系映射到表中。继承映射有三个原则: 

1、Table per class hierchy //每个类分层对应一个表
2、Table per subclass  //每个子类对应一个表
3、Table per concrete class //每个实体类对应一个表
下面以我做过的例子来讲一下我对这个的理解
首先是建一个po的对象 EMployee
package com.su02.teacheapp.hibernate.po;

import com.su02.teacheapp.beans.BaseBean;

public class Employee {
	private String id;
	private String ename;
	private String sex;
	
	public Employee(String ename, String sex) {
		super();
		this.ename = ename;
		this.sex = sex;
	}
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}

}

再就是建合同工人和正式工人的对象
package com.su02.teacheapp.hibernate.po;

public class NonContractEmployee extends Employee{
	private float yuebing;
	private int jidan;
	private String workwear;
	
	public NonContractEmployee(String ename, String sex, float yuebing, int jidan, String workwear) {
		super(ename, sex);
		this.yuebing = yuebing;
		this.jidan = jidan;
		this.workwear = workwear;
	}
	public NonContractEmployee() {
		super();
		// TODO Auto-generated constructor stub
	}
	public NonContractEmployee(String ename, String sex) {
		super(ename, sex);
		// TODO Auto-generated constructor stub
	}
	public int getJidan() {
		return jidan;
	}
	public void setJidan(int jidan) {
		this.jidan = jidan;
	}
	public String getWorkwear() {
		return workwear;
	}
	public void setWorkwear(String workwear) {
		this.workwear = workwear;
	}
	public float getYuebing() {
		return yuebing;
	}
	public void setYuebing(float yuebing) {
		this.yuebing = yuebing;
	} 

}

package com.su02.teacheapp.hibernate.po;

public class ContactEmployee extends Employee{
	private String startTime;
	private String endTime;
	
	public ContactEmployee() {
		super();
		// TODO Auto-generated constructor stub
	}
	public ContactEmployee(String ename, String sex) {
		super(ename, sex);
		// TODO Auto-generated constructor stub
	}
	public ContactEmployee(String ename, String sex, String startTime, String endTime) {
		super(ename, sex);
		this.startTime = startTime;
		this.endTime = endTime;
	}
	public String getEndTime() {
		return endTime;
	}
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	public String getStartTime() {
		return startTime;
	}
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

}

下面就开始先Employee的映射配置文件
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.su02.teacheapp.hibernate.po">
<class name="Employee" table="T_Employee_GD">
<id name="id">
<column name="id" sql-type="nvarchar2(32)"/>
<generator class="uuid"></generator>
</id>
<discriminator>
<column name="type" sql-type="number(1)"/>
</discriminator>
<property name="ename">
<column name="ename" sql-type="nvarchar2(10)"></column>
</property>
<property name="sex">
<column name="sex" check="sex in('男','女')"></column>
</property>
<subclass name="NonContractEmployee" discriminator-value="1">
<property name="yuebing">
<column name="yuebing" sql-type="number(2,1)"></column>
</property>
<property name="jidan">
<column name="jidan" sql-type="number(3)"></column>
</property>
<property name="workwear">
<column name="workwear" sql-type="nvarchar2(10)"></column>
</property>
</subclass>
<subclass name="ContactEmployee" discriminator-value="2">
<property name="startTime">
<column name="startTime" sql-type="nvarchar2(32)"></column>
</property>
<property name="endTime">
<column name="endTime" sql-type="nvarchar2(32)"></column>
</property>
</subclass>
</class>
</hibernate-mapping>
把你写的Employee.hbm.xml添加到你的cfg.xml文件中去下一个简单的测试类测试一下吧!

你可能感兴趣的:(Hibernate,xml,.net)