Hibernate Mapping:实体映射中的重复列

时间:2015-10-15 08:05:30

标签: java hibernate java-ee

我有两个类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>

Application.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 =&#34; false&#34;或者更新=&#34; false&#34;并且两个外键都应该映射到Employee表的employeeId。我该怎么办?

1 个答案:

答案 0 :(得分:0)

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

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

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