【问题标题】:Hibernate Mapping Optional ValueHibernate 映射可选值
【发布时间】: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


    【解决方案1】:

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

    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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      查看 Hibernate 参考文档的Association Mappings chapter,它解释了映射各种关联的几种不同方法,具体取决于关系是单向还是双向、一对一、一对一多或多对多,以及表之间的关联是直接关联还是通过连接表完成。

      【讨论】:

        【解决方案3】:

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

        类似:

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

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

        【讨论】:

          猜你喜欢
          • 2011-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-14
          • 1970-01-01
          • 2011-07-26
          • 1970-01-01
          • 2010-11-27
          相关资源
          最近更新 更多