Hibernate 映射可选值
我有两个类需要进行 XML 映射(最终它们都会被修改为 Annotations,但目前我们需要支持 XML 映射)。
我有一个 User 对象,当前如下所示:
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 很陌生,但这似乎应该很容易映射。我以为我已经正确完成了映射,但是我在尝试加载一个人时遇到反序列化异常(我已将这些类标记为可序列化 - 无济于事)。
I have two classes which need to be XML mapped (eventually they will all be modified to Annotations, but currently we need to support the XML mappings).
I have a User object which currently looks like this:
public class User {
private Key key;
private Name name;
}
I need to add in Preferences for some of these users (we have two different types of users which share the same object).
public class Preferences {
private Person person; //The person key acts as our foreign and primary key
private Integer numToShow;
private String defaultScreenToShow;
}
My person XML is as such:
<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>
My preferences XML file is as such:
<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>
I'm pretty green with Hibernate in all honesty, but this seems like something which should be pretty easy to map in. I thought I had the mappings done properly, but I get a deserialization exception upon trying to load a person (I've marked the classes as Serializable -- to no avail).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将其映射为仅包含一个组件的复合 ID。
类似于:
否则,您可以将其值设置为唯一并使用休眠的内置生成键,同时仍然能够根据 Person 查找单行。
Try mapping it as a composite id with only the one component.
Something like:
Otherwise, you could just set it's value to be unique and use the hibernate's built in generated key while still being able to look up a single row based on Person.
查看关联映射章节 Hibernate 参考文档,因为它解释了映射各种关联的几种不同方法,具体取决于关系是单向还是双向、一对一、一对多或多对多,以及表之间的关联是直接关联还是通过连接表完成。
Take a look at the Association Mappings chapter of the Hibernate reference documentation, as it explains several different ways to map all kinds of associations, depending on if the relationship is unidirectional or bidirectional, one-to-one, one-to-many, or many-to-many, and whether or not the association between the tables is direct or done through a join table.
我能够以我想要的方式解决这个问题(注释新类,并添加到旧类的 XML 中——当我在每种情况下使用 XML 时,我只能存储枚举的序号值,这是不希望)。
我能够注释的新课程现在看起来像:
I was able to solve this in my desired way (Annotating the new class, and adding to the XML of the old class -- When I used XML in each situation, I was only able to store the Ordinal value of the enum, which wasn't desired).
My new class, which I was able to annotate, now looks like: