休眠中一对一关联的延迟加载问题 [英] Lazy loading issue with One to one association in hibernate

查看:51
本文介绍了休眠中一对一关联的延迟加载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题与延迟加载 OneToOne 关联映射有关.

I have a question related to lazy loading of OneToOne association mapping.

情况1 外键在子表(地址)中

Case 1 Foreign key is in Child table (Address)

@Entity
public class User {
    ..........
    @OneToOne(mappedBy="user")
    private Address address;


@Entity
public class Address{
    .........
    @OneToOne
    @JoinColumn(name = "user_id")
    private User user;

在上述地址中,延迟加载无效.

In the above Address lazy loading doesn't work.

案例2 外键在父表(用户)中

Case 2 Foreign key is in Parent table (User)

@Entity
public class User {
    .............
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="address_id")
    private Address address;


@Entity
public class Address {
    ........
    @OneToOne(mappedBy="address")
    private User user;

在上述地址中,延迟加载有效.

In the above Address lazy loading works.

请有人解释一下为什么一对一延迟加载在第一种情况下不起作用,而在第二种情况下起作用吗?

Could please someone explain me why One to one lazy loading doesn't work in first case but works in second?

推荐答案

@OneToOne 有点棘手.

这完全取决于您使用的持久性提供程序.

It all depends on what persistence provider you're using.

某些提供程序不尊重 FetchType.LAZY 提示.

Some providers do not respect FetchType.LAZY hint.

您可以尝试指定(在关系的两端)

You can try to specify (on both ends of relation)

@OneToOne(optional = true, fetch = FetchType.LAZY)

要了解此处发生的情况,请看一下级别:

To understand what happens here lets take a look at the level:

第一种情况:

+-----------------+             +------------------+
| USER            |             |  Address         |
|                 |1           1|                  |
|                 +-------------+  USER_ID (FK)    |
|                 |             |                  |
|                 |             |                  |
+-----------------+             +------------------+

加载用户时,Hibernate必须知道该地址是否存在.

When you load the user Hibernate has to know if the Address is present or not.

因此Hibernate发出类似于以下内容的SQL请求:

So Hibernate issues a SQL request similar to this:

SELECT * FROM USER WHERE ID = ?
SELECT * FROM ADDRESS WHERE user_id = ?

获取结果时,实体已经加载,因此没有必要将LazyProxy分配给该地址.Hibernate分配获取的对象.

When getting the result the entity is already loaded, so it's no point to assign a LazyProxy to the Address. Hibernate assigns the fetched object.

第二种情况:

+-----------------+             +------------------+
| USER            |             |  Address         |
|                 |1           1|                  |
| ADDRESS_ID      +-------------+                  |
|                 |             |                  |
|                 |             |                  |
+-----------------+             +------------------+

SELECT * FROM USER WHERE ID = ?

休眠不需要检查地址是否存在.这就是创建代理的原因.

Hibernate does not need to check if the Address is there or not. That's why the proxy is created.

这篇关于休眠中一对一关联的延迟加载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆