【发布时间】: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