Hibernate 映射可选值

发布于 2024-11-08 07:35:08 字数 1592 浏览 0 评论 0原文

我有两个类需要进行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_畞蕅 2024-11-15 07:35:21

尝试将其映射为仅包含一个组件的复合 ID。

类似于:

<composite-id name="col_name">
   <key-many-to-one name="person" class="Person" column="person_col"/>
</composite-id>

否则,您可以将其值设置为唯一并使用休眠的内置生成键,同时仍然能够根据 Person 查找单行。

Try mapping it as a composite id with only the one component.

Something like:

<composite-id name="col_name">
   <key-many-to-one name="person" class="Person" column="person_col"/>
</composite-id>

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.

奢望 2024-11-15 07:35:20

查看关联映射章节 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.

惜醉颜 2024-11-15 07:35:19

我能够以我想要的方式解决这个问题(注释新类,并添加到旧类的 XML 中——当我在每种情况下使用 XML 时,我只能存储枚举的序号值,这是不希望)。

Person.hbm.xml
<join table="PREFS_JOIN_TABLE" optional="true">
    <key column="PERSON_ID" />
    <many-to-one name="preferences" column="PREFERENCES_ID" not-null="true" unique="true" cascade="all"/>
</join>

我能够注释的新课程现在看起来像:

@Entity
@Table(name = "PREFERENCES")
public class UserPreferences implements Preferences {
    private Long id;
    private Panel defaultPanelToShow;
    private Person person;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="prefSeq")
    @SequenceGenerator(name="prefSeq", sequenceName = "SQ_PREFERENCES_ID", allocationSize = 10, initialValue = 1)
    @Column(name="PREFERENCES_ID")
    public Long getId() {
        return id;
    }

    @Column(name="DEFAULT_USER_PANEL")
    @Enumerated(EnumType.STRING)
    public Panel getDefaultRequestPanel() {
        return defaultPanelToShow;
    }

    @OneToOne
    @JoinTable(name="PREFS_JOIN_TABLE", joinColumns=@JoinColumn(name="PREFERENCES_ID", unique=true), inverseJoinColumns=@JoinColumn(name="PERSON_ID", unique=true))
    public Person getPerson() {
        return person;
    }
}

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).

Person.hbm.xml
<join table="PREFS_JOIN_TABLE" optional="true">
    <key column="PERSON_ID" />
    <many-to-one name="preferences" column="PREFERENCES_ID" not-null="true" unique="true" cascade="all"/>
</join>

My new class, which I was able to annotate, now looks like:

@Entity
@Table(name = "PREFERENCES")
public class UserPreferences implements Preferences {
    private Long id;
    private Panel defaultPanelToShow;
    private Person person;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="prefSeq")
    @SequenceGenerator(name="prefSeq", sequenceName = "SQ_PREFERENCES_ID", allocationSize = 10, initialValue = 1)
    @Column(name="PREFERENCES_ID")
    public Long getId() {
        return id;
    }

    @Column(name="DEFAULT_USER_PANEL")
    @Enumerated(EnumType.STRING)
    public Panel getDefaultRequestPanel() {
        return defaultPanelToShow;
    }

    @OneToOne
    @JoinTable(name="PREFS_JOIN_TABLE", joinColumns=@JoinColumn(name="PREFERENCES_ID", unique=true), inverseJoinColumns=@JoinColumn(name="PERSON_ID", unique=true))
    public Person getPerson() {
        return person;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文