java - 热切/惰性加载成员始终为空,且具有 JPA 一对多关系

标签 java hibernate orm jpa lazy-loading

我有两个实体,一个用户和一个角色,具有从用户到角色的一对多关系。这些表格如下所示:

mysql> select * from User;
+----+-------+----------+
| id | name  | password |
+----+-------+----------+
|  1 | admin | admin    |
+----+-------+----------+
1 row in set (0.00 sec)

mysql> select * from Role;
+----+----------------------+---------------+----------------+
| id | description          | name          | summary        |
+----+----------------------+---------------+----------------+
|  1 | administrator's role | administrator | Administration |
|  2 | editor's role        | editor        | Editing        |
+----+----------------------+---------------+----------------+
2 rows in set (0.00 sec)

这是创建的连接表:

mysql> select * from User_Role;
+---------+----------+
| User_id | roles_id |
+---------+----------+
|       1 |        1 |
|       1 |        2 |
+---------+----------+
2 rows in set (0.00 sec)

这是定义表和关系的 orm.xml 子集:

<entity class="User" name="User">
    <table name="User" />
    <attributes>
        <id name="id">
            <generated-value strategy="AUTO" />
        </id>
        <basic name="name">
            <column name="name" length="100" unique="true" nullable="false"/>
        </basic>
        <basic name="password">
            <column length="255" nullable="false" />
        </basic>
        <one-to-many
            name="roles"
            fetch="EAGER"
            target-entity="Role"
            />
    </attributes>
</entity>

<entity class="Role" name="Role">
    <table name="Role" />
    <attributes>
        <id name="id">
            <generated-value strategy="AUTO"/>
        </id>
        <basic name="name">
            <column name="name" length="40" unique="true" nullable="false"/>
        </basic>
        <basic name="summary">
            <column name="summary" length="100" nullable="false"/>
        </basic>
        <basic name="description">
            <column name="description" length="255"/>
        </basic>
    </attributes>
</entity>

尽管如此,当我检索管理员用户时,我得到一个空集合。我使用 Hibernate 作为我的 JPA 提供程序,它显示以下调试 SQL:

select
    user0_.id as id8_,
    user0_.name as name8_,
    user0_.password as password8_ 
from
    User user0_ 
where
    user0_.name=? limit ?

当一对多映射被延迟加载时,这是唯一进行的查询。这会正确检索一个管理员用户。我将关系更改为使用预加载,然后除了上面的查询之外还进行了以下查询:

select
    roles0_.User_id as User1_1_,
    roles0_.roles_id as roles2_1_,
    role1_.id as id9_0_,
    role1_.description as descript2_9_0_,
    role1_.name as name9_0_,
    role1_.summary as summary9_0_ 
from
    User_Role roles0_ 
left outer join
    Role role1_ 
        on roles0_.roles_id=role1_.id 
where
    roles0_.User_id=?

这会产生以下结果:

+----------+-----------+--------+----------------------+---------------+----------------+
| User1_1_ | roles2_1_ | id9_0_ | descript2_9_0_       | name9_0_      | summary9_0_    |
+----------+-----------+--------+----------------------+---------------+----------------+
|        1 |         1 |      1 | administrator's role | administrator | Administration |
|        1 |         2 |      2 | editor's role        | editor        | Editing        |
+----------+-----------+--------+----------------------+---------------+----------------+
2 rows in set (0.00 sec)

Hibernate 显然知道角色,但 getRoles() 仍然返回一个空集合。 Hibernate 也充分认识到这种关系,将数据放在首位。

什么问题会导致这些症状?

最佳答案

对我来说,您的物理模型和实体的映射之间存在某种不匹配:物理模型实现多对多关系(使用连接表),而映射声明一对多关系关系。 IMO,物理模型是“正确的”:一个用户可以有多个角色,一个角色可以与多个用户关联。换句话说,用户和角色之间是多对多的关系。

关于java - 热切/惰性加载成员始终为空,且具有 JPA 一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2610113/

相关文章:

java - 向 LINE Notify 发送消息失败(使用 HttpsURLConnection POST)

java - Spring Boot项目构建成功并报错

java - 使用 Hibernate 获取具有最大 id 值的实体

c# - 我应该使用 ORM 来为大型树结构建模吗?

java - 当我使用列表时出现奇怪的空指针异常错误

java - Apache POI HSSF : How to add shape which would not resize along with corresponding cell?

orm - 使用 laravel eloquent 从两个连接表中获取数据

php - Doctrine:将模型值设置为数组

java - View 的动画集

java - Spring MVC + Hibernate - 从表单更新一些属性