3
我有以下實體映射:爲什麼我的多級繼承映射不能像我期望的那樣工作?
// user.cfc
component persistent="true" table="user" discriminatorColumn="userTypeID" {
property name="id" column="userID" fieldtype="id" generator="identity";
property name="type" fieldtype="many-to-one" cfc="userType" fkcolumn="userTypeID";
}
// admin.cfc
component extends="user" persistent="true" table="admin" joincolumn="userID" discriminatorValue="3" {
property name="id" column="adminID" fieldtype="id" generator="identity";
}
// employee.cfc
component extends="user" persistent="true" table="employee" joincolumn="userID" discriminatorValue="0" {
property name="id" column="employeeID" fieldtype="id" generator="identity";
}
// manager.cfc
component extends="employee" persistent="true" table="manager" joincolumn="employeeID" discriminatorValue="1" {
property name="id" column="managerID" fieldtype="id" generator="identity";
}
// intern.cfc
component extends="employee" persistent="true" table="intern" joincolumn="employeeID" discriminatorValue="2" {
property name="id" column="internID" fieldtype="id" generator="identity";
}
每亨利的建議,這裏是產生hbmxml文件:
<!-- user.hbmxml -->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="user" lazy="true"
name="cfc:user" table="user">
<id name="ID" type="int">
<column name="userID"/>
<generator class="identity"/>
</id>
<discriminator column="userTypeID"/>
<many-to-one class="cfc:userType"
column="userTypeID" insert="false" name="type" update="false"/>
</class>
</hibernate-mapping>
<!-- admin.hbmxml -->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<subclass discriminator-value="3" entity-name="admin"
extends="cfc:user" lazy="true" name="cfc:admin">
<join table="admin">
<key column="userID"/>
</join>
</subclass>
</hibernate-mapping>
<!-- employee.hbmxml -->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<subclass discriminator-value="0" entity-name="employee"
extends="cfc:user" lazy="true" name="cfc:employee">
<join table="employee">
<key column="userID"/>
</join>
</subclass>
</hibernate-mapping>
<!-- manager.hbmxml -->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<subclass discriminator-value="1" entity-name="manager"
extends="cfc:employee" lazy="true" name="cfc:manager">
<join table="manager">
<key column="employeeID"/>
</join>
</subclass>
</hibernate-mapping>
<!-- intern.hbmxml -->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<subclass discriminator-value="2" entity-name="intern"
extends="cfc:employee" lazy="true" name="cfc:intern">
<join table="intern">
<key column="employeeID"/>
</join>
</subclass>
</hibernate-mapping>
如果不是從映射清晰,關係如下:
user
|- admin
|- employee
|- manager
|- intern
意圖是對user
實體上的type
屬性進行填充manager
和intern
實體的鑑別器值。 employee
的構造函數中存在代碼,使其不能直接實例化,所以user
將始終有一個type
。
從數據庫讀取一些已經存在的數據時,整個映射工作正常。但是,當我試圖插入新記錄時遇到問題。
假設已經涉及到的表有一些記錄填充:
user
|---------------------|
| userID | userTypeID |
|---------------------|
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
| 4 | 3 |
|---------------------|
admin
|------------------|
| adminID | userID |
|------------------|
| 1 | 4 |
|------------------|
employee
|---------------------|
| employeeID | userID |
|---------------------|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
|---------------------|
manager
|------------------------|
| managerID | employeeID |
|------------------------|
| 1 | 1 |
|------------------------|
intern
|-----------------------|
| internID | employeeID |
|-----------------------|
| 1 | 2 |
| 2 | 3 |
|-----------------------|
如果我要創建一個新的intern
實體和堅持它,我希望要插入三個記錄:
INSERT user (userID, userTypeID) VALUES (5, 2)
INSERT employee (employeeID, userID) VALUES (4, 5)
INSERT intern (internID, employeeID) VALUES (3, 4)
但是,實際執行的SQL如下所示:
INSERT user (userID, userTypeID) VALUES (5, 2)
INSERT employee (employeeID, userID) VALUES (4, 5)
INSERT intern (internID, employeeID) VALUES (3, 5) -- using the new userID instead of the new employeeID
最後,實際問題:
爲什麼在插入到intern
時,是使用userID
而不是employeeID
?就好像Hibernate忽略intern
上的joincolumn
屬性並僅使用employee
中的joincolumn
。
爲了讓hibernate的人幫助你,你可能要考慮發佈'.hbxml'生成的CF。 – Henry
@亨利 - 好主意,補充。 –
我可能是錯的,但我相信這就是休眠的工作原理。他們有這個背後的原因。所以要麼喝酒,要留下他們給你的東西,或者簡化你的設計。 – Henry