Hibernate一对多映射问题

w2qiao 发布于 2015/05/10 16:52
阅读 77
收藏 0

有一个继承类的映射文件如下


<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate的映射文件的DTD信息 -->
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.crazyit.app.domain">
	<!-- 映射Person类 -->	
	<class name="Person" table="person_inf">
		<!-- 映射标识属性 -->
		<id name="id" column="person_id">
			<!-- 不能使用identity的主键生成器策略
				,所以使用hilo主键生成器策略-->
			<generator class="hilo"/>
		</id>
		<!-- 如下映射两个基本属性 -->
		<property name="name" length="80"/>
		<property name="gender"/>
		<!-- 下面映射了一个组件属性 -->
		<component name="address">
			<!-- 映射组件属性的三个成员属性 -->
			<property name="detail"/>
			<property name="zip"/>
			<property name="country"/>
		</component>
		<!-- 使用union-subclass元素映射Person类的Employee子类 -->
		<union-subclass name="Employee" table="employee_inf">
			<!-- 映射Employee类的两个普通属性 -->
			<property name="title" not-null="true"/>
			<property name="salary" not-null="true"/>
			<!-- 映射Employee类与Manager类之间的N-1关联-->
			<many-to-one name="manager" column="manager_id"/>
			<!-- 映射Employee类与Customer类之间的1-N关联-->
			<set name="customers" inverse="true">
				<key column="employee_id"/>
				<one-to-many class="Customer"/>
			</set> 
			<!-- 使用union-subclass元素映射Employee类的Manager子类 -->
			<union-subclass name="Manager" table="manager_inf">
				<!-- 映射Manager类的department属性 -->
				<property name="department"/>
				<!-- 映射Employee类与Manager类之间的1-N关联-->
				<set name="employees" inverse="true">
					<key column="manager_id"/>
					<one-to-many class="Employee"/>
				</set> 
			</union-subclass>
		</union-subclass>
		<!-- 使用union-subclass元素映射Person类的Customer子类 -->
		<union-subclass name="Customer" table="customer_inf">
			<property name="comments" not-null="true"/>
			<!-- 映射Employee类与Customer类之间的1-N关联-->
			<many-to-one name="employee" column="employee_id"/>
		</union-subclass>
	</class>
</hibernate-mapping>


在customer_inf的地方,应该有一个外键到employee_inf才对啊,控制台输出

Hibernate:
    alter table employee_inf
        add constraint FK_mao9jvcblb8qlcpyxa3n7av9p
        foreign key (manager_id)
        references manager_inf (person_id)
Hibernate:
    alter table manager_inf
        add constraint FK_n4pyo72xuefom13wrqc5fsdlk
        foreign key (manager_id)
        references manager_inf (person_id)

没有给customer添加外键,

<many-to-one name="employee" column="employee_id"/>



many-to-one应该在many的一端添加一个外键,来维护关系的呀?

加载中
返回顶部
顶部