我有这些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中映射它们。
我尝试了这种映射:
<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>
如果我只有table1,这可以正常工作,当我为table2添加相同的映射时, 我收到这个错误:
org.hibernate.MappingException: Repeated column in mapping for entity: Error column:
TABLE_ID (should be mapped with insert="false" update="false")
请帮我正确映射。
由于
答案 0 :(得分:0)
任何映射都适用于此
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>