java - Hibernate:一对一映射无法正常工作

标签 java sql spring hibernate oracle12c

我在 Spring Web 应用程序中使用 Hibernate 框架来处理 Oracle 数据库。

我有这两个相互关系的实体: pseudo ERD diagram

基本上,每个人可以有零个或一个用户,并且用户必须有一个人。

我想实现一种行为,当我从数据库中选择某个用户时,我会将 Person 实体映射到它。

这是我的 POJO:

public class User implements Serializable {

    private String id = null;
    private String username = null;
    private String password = null;
    private boolean enabled = false;
    private Person person = null;
    private LocalDateTime created = null;

    // getters and setters
}

public class Person implements Serializable {

    private String id = null;
    private String firstName = null;
    private String lastName = null;

    // getters and setters
}

下面是相应的配置 xml 文件:

<hibernate-mapping package="webapp.models">
    <class name="webapp.models.User"
           table="USERS">

        <id name="id"
            type="java.lang.String">
            <column name="ID"
                    not-null="true"
                    unique="true"/>
            <generator class="guid"/>
        </id>

        <property name="username"
                  type="java.lang.String">
            <column name="USERNAME"
                    not-null="false"
                    unique="false"/>
        </property>

        <property name="password"
                  type="java.lang.String">
            <column name="PASSWORD"
                    not-null="false"
                    unique="false"/>
        </property>

        <property name="enabled"
                  type="java.lang.Boolean">
            <column name="ENABLED"
                    not-null="false"
                    unique="false"/>
        </property>

        <property name="created"
                  type="webapp.utils.hibernate.LocalDateTimeUserType">
            <column name="CREATED"
                    not-null="false"
                    unique="false"/>
        </property>

        <one-to-one name="person"
                    class="Person"
                    fetch="select"
                    lazy="false">
        </one-to-one>
    </class>
</hibernate-mapping>

<hibernate-mapping package="webapp.models">
    <class name="webapp.models.Person"
           table="PERSONS">

        <id name="id"
            type="java.lang.String">
            <column name="ID"
                    not-null="true"
                    unique="true"/>
            <generator class="guid"/>    
        </id>

        <property name="firstName"
                  type="java.lang.String">
            <column name="FIRST_NAME"
                    not-null="false"
                    unique="false"/>
        </property>

        <property name="lastName"
                  type="java.lang.String">
            <column name="LAST_NAME"
                    not-null="false"
                    unique="false"/>
        </property>

        <property name="created"
                  type="webapp.utils.hibernate.LocalDateTimeUserType">
            <column name="CREATED"
                    not-null="false"
                    unique="false"/>
        </property>
    </class>
</hibernate-mapping>

我的问题是 user 中的 person 属性始终设置为 null 即使相应的 Person 存在。

执行代码如下:

public Collection<User> getAll() {
    Session session = null;
    try {
        session = _sessionFactory.openSession();
        Query query = session.createQuery("from User fetch all properties");
        List<User> collection = query.list();
        return Collections.unmodifiableCollection(collection);
    } catch (HibernateException hbEx) {
        return null;
    } finally {
        if (session != null && session.isOpen())
            session.close();
    }
}

我使用 SQL 查询和绑定(bind)打开了 Hibernate 日志记录,我发现当它从数据库中选择一个 Person 时,它使用如下查询:

select
    person0_.ID as ID1_0_0_,
    person0_.FIRST_NAME as FIRST_NAME2_0_0_,
    person0_.LAST_NAME as LAST_NAME3_0_0_,
    person0_.CREATED as CREATED4_0_0_ 
from
    PERSONS person0_ 
where
    person0_.ID=?

并且在 WHERE 子句中将 person0_.ID 设置为用户的 ID,因此它不会采用 Users 表中的 PERSON_ID 外键。

我是否设置错误或者问题出在哪里?

预先感谢您的帮助。

最佳答案

一对一关系必须确保两个实体分配有相同的主键。您应该声明特殊的外部标识符生成器,​​当 constrained=”true” 时,它将从另一个表获取主键值。

    <id name="id" type="java.lang.Integer">
        <column name="id" />
        <generator class="foreign">
            <param name="property">person</param>
        </generator>
    </id>

关于java - Hibernate:一对一映射无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30914848/

相关文章:

spring - 为 Rest Api 实现 Spring Security

java - 递归函数中的 If 子句

SQLite 查询以获取最近的日期时间

mysql - 多对多SQL select查询来获取匹配的记录

sql - 检查名称是否包含多个标题

java - 使用Spring Data Gemfire时的@Region注释

java - 在父 JFrame 处于最大化状态时关闭 JPopupMenu 时出现问题

用于加载对象的 Java 接口(interface)

java - 监听器和计时器之间的通信

java - 应用 @ResponseBody 时出现循环 View 路径错误