【问题标题】:Hibernate one-to-one mapping with a reference column (XML mapping)Hibernate 与引用列的一对一映射(XML 映射)
【发布时间】:2013-02-04 07:29:59
【问题描述】:

我有一个用户表和一个具有一对一映射的user_detailuser_detail 表有一个字段user_id 用于存储相应用户的id 字段值的关系。

如何为这个关系编写hibernate hbm文件?

更新

我的问题是用户的主键是id,user_detail的外键是user_id

我在互联网用户 user_id 中获得的所有示例作为用户主键,与其他表中的外键相同

【问题讨论】:

  • 你试过了吗?这非常简单,您可以在众多 Hibernate 教程之一中找到一个示例。
  • 我看了很多例子,但都给出了相同的情况,在我的例子中,我们总是将 PK 作为 id,如何定义 user_detail 表的字段 user_id 与用户表 id 字段相关
  • @RaulGogo :我已经编辑了问题以使其清楚
  • 发布了一个实际上解决了上述要求的答案。

标签: java database hibernate one-to-one


【解决方案1】:

在 User.hbm 中,您需要像这样声明与 UserDetails 的关系:

<one-to-one name="user_detail" foreign-key="user_id" class="UserDetail full name "/>

希望对你有帮助

【讨论】:

    【解决方案2】:

    用于用户映射....

    <?xml version="1.0"?>
            <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
            <!-- Generated 25 April 2011 7:52:33 PM by Hibernate Tools 3.4.0.CR1 -->
            <hibernate-mapping>
                <class name="com.rais.User" table="USER" catalog="mydb">
                    <id name="userId" type="java.lang.Integer">
                        <column name="USER_ID" />
                        <generator class="identity" />
                    </id>
                    <property name="userName" type="string">
                        <column name="USER_NAME" length="10" not-null="true" unique="true" />
                    </property>
                    <property name="userCode" type="string">
                        <column name="USER_CODE" length="20" not-null="true" unique="true" />
                    </property>
                    <one-to-one name="userDetail" class="com.rais.UserDetail"
                        cascade="save-update"></one-to-one>
                </class>
            </hibernate-mapping>
    

    用于 UserDetail 映射。

        <?xml version="1.0"?>
        <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
        <!-- Generated 25 April 2011 7:52:33 PM by Hibernate Tools 3.4.0.CR1 -->
        <hibernate-mapping>
            <class name="com.rais.UserDetail" table="USER_DETAIL"
                catalog="mydb">
                <id name="userId" type="java.lang.Integer">
                    <column name="USER_ID" />
                    <generator class="foreign">
                        <param name="property">user</param>
                    </generator>
                </id>
                <one-to-one name="user" class="com.rais.User"
                    constrained="true"></one-to-one>
                <property name="compName" type="string">
                    <column name="COMP_NAME" length="100" not-null="true" />
                </property>
                <property name="compDesc" type="string">
                    <column name="COMP_DESC" not-null="true" />
                </property>
                <property name="remark" type="string">
                    <column name="REMARK" not-null="true" />
                </property>
            </class>
        </hibernate-mapping>
    

    【讨论】:

    • 它的多对一关系不,我需要一对一,而且用户主键名称是id,user_detail中的外键是user_id
    • 你明白我需要什么吗?
    • @prashu132 修改答案以满足您的要求。请看上面的代码。
    • 谢谢老兄&lt;generator class="foreign"&gt; &lt;param name="property"&gt;user&lt;/param&gt; &lt;/generator&gt; 是我需要的东西。现在工作正常。
    • 能否提供一个示例代码,用于使用 user_detail 为用户创建和更新
    【解决方案3】:

    对于 UserDetail 的主键不是外键的一对一关联,我们必须将其表示为拥有方的多对一关联。

    来自 Hibernate 文档https://docs.jboss.org/hibernate/orm/3.2/reference/en/html/mapping.html

    或者,具有唯一约束的外键,从 Employee 到 Person,可以表示为:

    &lt;many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/&gt;

    并且可以通过将以下内容添加到 Person 映射来建立这种关联:

    &lt;one-to-one name"employee" class="Employee" property-ref="person"/&gt;

    因此,无法使用一对一映射此关联,您必须在拥有方(表持有外键)将映射更改为多对一。

    【讨论】:

      【解决方案4】:
      <one-to-one name="user_detail" class="give the full specified className with package declaration" cascade="save-update"></one-to-one>
      

      这是一个很好的例子

      http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example/

      请找到它。

      【讨论】:

      • 我看了很多例子,但都给出了相同的情况,在我的例子中,我们总是给 PK 作为id,如何定义 user_detail 表的字段 user_id 与用户表id 字段相关
      • 对我的场景有任何想法吗?
      • 你提供的链接也是我开始时检查的第一件事
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 1970-01-01
      • 2021-08-18
      • 2012-04-21
      • 1970-01-01
      相关资源
      最近更新 更多