我有两个类似的东西:
public class Message implements Serializable {
...
private int _messageId;
private String _message;
private String type;
...
和
public class MyClass implements Serializable {
private final int myClassId;
...
private final List<Message> messagesOfTypeA;
private final List<Message> messagesOfTypeB;
private final List<Message> messagesOfTypeC;
...
我想使用hibernate来保存这两个类。 字段messagesOfTypeA,messagesOfTypeB和messagesOfTypeC根据Message中的类型字段对消息对象进行分组。
如何使用XML hibernate映射映射MyClass和Message之间的这种一对多关系? (我使用的是Hibernate 3.6)
答案 0 :(得分:0)
<强> MyClass的强>
<hibernate-mapping>
<class name="MyClass" table="my_class">
<id name="myClassId" column="id">
<generator class="native"/>
</id>
<list name="messagesOfTypeA" cascade="all" fetch="join">
<key column="myclass_id"/>
<list-index column="idx"/>
<one-to-many class ="Message"/>
</list>
<list name="messagesOfTypeB" cascade="all" fetch="join">
<key column="myclass_id"/>
<list-index column="idx"/>
<one-to-many class ="Message"/>
</list>
<list name="messagesOfTypeC" cascade="all" fetch="join">
<key column="myclass_id"/>
<list-index column="idx"/>
<one-to-many class ="Message"/>
</list>
</class>
</hibernate-mapping>
<强>消息强>
<hibernate-mapping>
<class name="Message" table="message">
<id name="_messageId" column="id">
<generator class="native"/>
</id>
<property name="_message" column="message"/>
<property name="type"/>
</class>
</hibernate-mapping>
您最终会使用类型判别式和一个FK的表消息 到 MY_CLASS 表。
CREATE TABLE "MESSAGE"
( "ID" NUMBER(10,0) NOT NULL ENABLE,
"MESSAGE" VARCHAR2(255 CHAR),
"TYPE" VARCHAR2(255 CHAR),
"MYCLASS_ID" NUMBER(10,0),
"IDX" NUMBER(10,0),
PRIMARY KEY ("ID")
CONSTRAINT "FK_MY_CLASS" FOREIGN KEY ("MYCLASS_ID")
REFERENCES "MY_CLASS" ("ID") ENABLE
)