【发布时间】:2011-05-18 20:06:33
【问题描述】:
我有两个类需要进行 XML 映射(最终它们都会被修改为 Annotations,但目前我们需要支持 XML 映射)。
我有一个用户对象,目前看起来像这样:
public class User {
private Key key;
private Name name;
}
我需要为这些用户中的 一些 添加首选项(我们有两种不同类型的用户共享同一个对象)。
public class Preferences {
private Person person; //The person key acts as our foreign and primary key
private Integer numToShow;
private String defaultScreenToShow;
}
我的人 XML 是这样的:
<hibernate-mapping package="com.example.entities">
<id key column="PERSON_ID" /> <!-- Leaving out custom generator -->
<!--
Not sure what the column needs to be here, as
preferences are in own table. Also read it has to
be a faked out many-to-one here as not all users will
have preferences.
-->
<many-to-one name="preferences" not-null="false" />
<component class="com.example.entities.Name">
<property column="first_name" name="first" />
<property column="last_name" name="last" />
</component>
</hibernate-mapping>
我的偏好 XML 文件是这样的:
<hibernate-mapping package="com.example.entities">
<property column="default_screen" name="defaultScreenToShow" />
<property column="number_search_results" name="numToShow" />
<!-- Not sure what the ID needs to be here -->
</hibernate-mapping>
老实说,我对 Hibernate 非常熟悉,但这看起来应该很容易映射。我认为我的映射已经正确完成,但是在尝试加载一个人时我得到了一个反序列化异常(我已将这些类标记为可序列化 - 无济于事)。
【问题讨论】:
标签: java hibernate hibernate-mapping