【问题标题】:Hibernate Mapping : Repeated column in mapping for entity休眠映射:实体映射中的重复列
【发布时间】:2015-10-15 08:05:30
【问题描述】:

我有两个类 Employee 和 Application。 员工有一个employeeId(PK)。应用程序有两个字段employeeId(fk) managerId(fk)。 employeeId 和 managerId 都应该引用 Employee 类的employeeId。所以我在各自的 hbm.xml 文件中有以下映射:

Employee.hbm.xml

   <hibernate-mapping package="com.quinnox.resignation2.0.model">
        <class name="Employee" table="Employee">
            <id name="employeeId" type="int">
                <generator class="native"></generator>
            </id> 
</class>
<hibernate-mapping>

应用程序.hbm.xml

<hibernate-mapping package="com.quinnox.resignation2.0.model">
    <class name="Application" table="Application">
        <id name="applicationId" type="int">
            <generator class="native"></generator>
        </id>
        <many-to-one name="empId" class="Employee" column="employeeId" />
        <many-to-one name="managerId" class="Employee"
            column="employeeId" />
</class></hibernate-mapping>

我还创建了适当的 POJO。当我尝试运行应用程序时,我收到以下错误

org.hibernate.MappingException: Repeated column in mapping for entity: com.quinnox.resignation2.0.model.Application column: employeeId (should be mapped with insert="false" update="false")

我不能设置 insert="false" 或 update="false" 并且两个外键都应该映射到 Employee 表的 employeeId。我该怎么办?

【问题讨论】:

  • 为什么您的设计需要将两列映射到一个外键,最好只有一个外键(尽管命名适当)
  • @SajanChandran 不,我需要将其引用到employeeId 本身,因为经理本人也是一名员工

标签: java hibernate jakarta-ee


【解决方案1】:
 <many-to-one name="managerId" class="Employee"
        column="employeeId" />

可能应该引用managerId 列而不是employeeId

 <many-to-one name="managerId" class="Employee"
        column="managerId" />

【讨论】:

  • 不,我需要将它引用到employeeId本身,因为经理本人也是一名员工
  • 然后只需添加到 insert="false" update="false" 到
猜你喜欢
  • 1970-01-01
  • 2012-03-11
  • 2016-11-20
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
相关资源
最近更新 更多