hibernate---映射文件,关系构建错误

严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.company.cosystem.domain.Manager column: dept_id (should be mapped with insert="false" update="false")


有个Employee,它的子类Manager,还有一个Department:
最开始认为Employee和Department存在N-1的关系,Department和Manger存在1-1的关系,Employee持有一个department字段,Department持有一个manager,manager是一个特殊的employee,无法创建相应数据表,因为彼此要求以对方的主键为外键。只好把Department的manager去掉,保留了一个String类型的managerName

Employee和Department的关联关系设置
<!-- 映射和Department的关联关系 -->
		<many-to-one name="dept" column="dept_id" class="Department"
			lazy="false" not-null="true" />

Manger和Department是1-1关联(么考虑副经理什么的),认为以下代码会覆盖父类关联关系,想当然了, 出现标红错误提示,因为这里又映射了一行dept_id,与上面的行dept_id重复
<subclass name="Manager" discriminator-value="2">
			<!-- 映射和Department的关联关系 -->
			<many-to-one name="dept" column="dept_id" unique="true"
				class="Department" lazy="false" not-null="true" />



删除第二段代码,为了获取部门经理,可判断emp_type=discriminator-value="经理的设置值"
<discriminator column="emp_type" type="int" />

你可能感兴趣的:(java,Hibernate)