休眠:多家长对孩子映射
我有这些Java类:休眠:多家长对孩子映射
Class Table1
{ Integer id;
Set<Error> errors;
}
Class Table2
{ Integer id;
Set<Error> errors;
}
Class Table3
{ Integer id;
Set<Error> errors;
}
Class Error
{
Integer id;
Integer tableId; // Pk of parent id
String tableName;
String errorMessage
}
如何最好地在休眠它们映射。
我想这个映射:
<hibernate-mapping>
<class name="Table1" table="TABLE1" schema="xxx" catalog="XXXXX">
<id name="id" type="integer">
<column name="ID" />
<generator class="native" />
</id>
<version column="MODIFIED_DATE" name="modifiedDate"
type="timestamp" unsaved-value="null"/>
<set name="Errors" table="ERROR" fetch="join"
lazy="false" cascade="all-delete-orphan" where="TABLE_NAME='table1'">
<key column ="TABLE_ID" not-null="true" />
<one-to-many class="Error" />
</set>
</class>
对于孩子:
<hibernate-mapping>
<class name="Error" table="ERROR" schema="XXX" catalog="xxxxx">
<id name="id" type="integer">
<column name="ID" />
<generator class="native" />
</id>
<version column="MODIFIED_DATE" name="modifiedDate" type="timestamp" unsaved-value="null"/>
<property name="tableId" type="integer" insert="false" update="false" >
<column name="TABLE_ID" not-null="true" />
</property>
<property name="tableName" type="string">
<column name="TABLE_NAME" not-null="true" length="20" />
</property>
<property name="errorMessage" type="string">
<column name="ERROR_MESSAGE" length="100" />
</property>
</class>
这工作得很好,如果我有只表1,当我添加相同的映射对于表2, 我收到此错误:
org.hibernate.MappingException: Repeated column in mapping for entity: Error column:
TABLE_ID (should be mapped with insert="false" update="false")
请帮助我正确的映射。
感谢
任何映射将适用此
Class Table1 implements HasErrors
{ Integer id;
Set<Error> errors;
}
Class Table2 implements HasErrors
{ Integer id;
Set<Error> errors;
}
Class Table3 implements HasErrors
{ Integer id;
Set<Error> errors;
}
Class Error
{
Integer id;
HasErrors parent;
String errorMessage
}
Integer tableId; // Pk of parent id
String tableName;
<class name="Error" table="ERROR" schema="XXX" catalog="xxxxx">
...
<any name="parent" id-type="integer" meta-type="String">
<meta-value value="Table1" class="Table1"/>
<meta-value value="Table2" class="Table2"/>
<column name="TABLE_NAME"/>
<column name="TABLE_ID"/>
</any>
<property name="errorMessage" type="string">
<column name="ERROR_MESSAGE" length="100" />
</property>
</class>
<class name="Table1" table="TABLE1" schema="xxx" catalog="XXXXX">
...
<set name="errors" table="ERROR" lazy="false" cascade="all-delete-orphan" where="TABLE_NAME='table1'">
<key column ="TABLE_ID" not-null="true" />
<many-to-any id-type="integer" meta-type="String">
<meta-value value="Table1" class="Table1"/>
<meta-value value="Table2" class="Table2"/>
<column name="TABLE_NAME" not-null="true"/>
<column name="TABLE_ID" not-null="true"/>
</many-to-any>
</set>
</class>
嗨Firo,谢谢你的回复。我试过这个映射,但我得到这个错误:org.hibernate.MappingException:无法解释元值。你能否详细说明或指出一些文件的使用?界面有什么方法?我试图用一个鉴别器“表名”在一个错误表中存储来自不同表(7)的错误。谢谢。 – Akhil 2012-02-14 22:36:23
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-types-anymapping – Firo 2012-02-15 06:41:02
嗨Firo,感谢您的链接。我得不到多少帮助。我仍然无法弄清楚Child表映射中的错误:rg.hibernate.MappingException:无法解释元值。你能指点我正确的方向吗?谢谢。 – Akhil 2012-02-16 18:37:56
可以告诉你,让你的错误 – frictionlesspulley 2012-02-14 04:15:09
我得到这个错误时,我有表2相同表1,刚刚更换table1的映射中的映射与table2。谢谢。 – Akhil 2012-02-14 04:44:10