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

[color=red]严重: 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")[/color]

有个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关联(么考虑副经理什么的),认为以下代码会覆盖父类关联关系,想当然了,[b]出现标红错误提示,因为这里又映射了一行dept_id,与上面的行dept_id重复[/b]
<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" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值