2015-10-15 304 views
0

我有兩個類Employee和Application。 員工有一個employeeId(PK)。應用程序有兩個字段employeeId(fk)managerId(fk)。 employeeId和managerId都應該引用Employee類的employeeId。所以我必須在各自的hbm.xml文件以下映射:Hibernate映射:實體映射中的重複列

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") 

我無法設置插入=「假」或更新=「假」和兩個外鍵應該映射到Employee表的僱員。我該怎麼辦?

+0

爲什麼您的設計需要映射到一個外鍵,它會更好,兩列,只有一個外鍵(適當雖然命名) –

+0

@SajanChandran不,我需要引用它到employeeId本身,因爲經理本身也是員工 –

回答

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

也許它應該引用managerId列,而不是employeeId

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

不,我需要引用它到employeeId本身,因爲管理者本身也是員工 –

+0

然後只需添加到insert =「false」 update =「false」爲<多對一名稱=「法力gerId「...> – StanislavL