简体   繁体   English

延迟加载不工作 Spring 数据 3.0.0 Hibernate - 在 Spring 引导迁移到 3.0 之后

[英]LAZY Loading not working Spring Data 3.0.0 Hibernate - After Spring Boot Migration to 3.0

After migrating to Spring Boot 3.0 our integration test showed that deleting a user did not work properly.迁移到 Spring Boot 3.0 后,我们的集成测试表明删除用户无法正常工作。

The User has two properties, where both are defined the same (at least thats what I think, see below). User 有两个属性,两者的定义相同(至少我是这么认为的,见下文)。

What I figured is, when I load the User object one of the properties of User are not loaded.我想的是,当我加载用户 object 时,未加载用户的属性之一。 Only if I define the property, which can not be loaded, as EAGER.仅当我将无法加载的属性定义为 EAGER 时。 If I now try to delete the User, this does not work because there is still the one property in DB which reference to the User.如果我现在尝试删除用户,这将不起作用,因为数据库中仍然有一个引用用户的属性。

I can not figure out whats the problem maybe I am missing something我不知道是什么问题,也许我遗漏了什么

My User Entity我的用户实体

@Entity
@Data
@Table(name = "user_model")
public class User extends BaseEntity {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;
    
    ...

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
    private UserSettings userSettings;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
    private UserTermsOfUse userTermsOfUse;
    
    ...
}

User Terms Of Use which works to load可以加载的用户使用条款

@Entity
@Table(name = "user_termsofuse")
@Data
public class UserTermsOfUse extends BaseEntity {

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    @ToString.Exclude
    private User user;

    @Column(name = "accepted_termsofuse")
    private String acceptedTermsOfUse;

}

My Settings which does not load:我的设置不加载:

@Entity
@Table(name = "user_settings")
@Data
public class UserSettings extends BaseEntity {

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    @ToString.Exclude
    private User user;

    private String language;

    private String lastActiveClientId;

}

As you can see in my screenshot, when I load the entity User by ID it only shows the User Terms of Use ant not the User Settings正如您在我的屏幕截图中所见,当我按 ID 加载实体用户时,它仅显示用户使用条款 ant 而不是用户设置在此处输入图像描述

Just to make clear, they are within the DB:)只是为了清楚起见,它们在数据库中:) 在此处输入图像描述

Hope Someone can point me to a correct direction希望有人能指出我正确的方向

UPDATE更新

I made a sample project to prove it does not work.我做了一个示例项目来证明它不起作用。 The test fails with an NullPonterException once I try to access the UserSettings .一旦我尝试访问UserSettings ,测试就会失败并出现NullPonterException

https://github.com/de313e/SpringEagerLoadingIssue https://github.com/de313e/SpringEagerLoadingIssue

This is clearly a bug for hibernate project.这显然是 hibernate 项目的错误。

Have opened a ticket for it here with both the bug described in the question from kism3t and also with what seems to be a workaround to bypass the issue.kism3t的问题中描述的错误以及似乎是绕过该问题的解决方法的问题都在这里打开了一张票。

Have checked the current non stable released versions of hibernate 6.2.0 and the bug is still there.检查了 hibernate 6.2.0的当前非稳定发布版本,错误仍然存在。 So probably is undetected yet.所以可能还没有被发现。

For the moment the following workaround seems to bypass the issue目前,以下解决方法似乎可以绕过该问题

   @Entity
   public class User {

      @OneToOne(fetch = FetchType.LAZY, mappedBy = "user") 
      private UserSettings userSettings;

      @OneToOne(fetch = FetchType.LAZY, mappedBy = "user") 
      private UserTermsOfUse userTermsOfUse;
  }

If the owning side of the @OneToOne declares the same fetchType of Lazy somehow the issue is bypassed.如果@OneToOne的拥有方声明了相同的Lazy fetchType,则该问题会以某种方式被绕过。

So it works with所以它适用于

@Entity
public class UserTermsOfUse {

    ....

    @OneToOne(fetch = FetchType.LAZY)  // <-------------
    private User user;
}

and

@Entity
public class UserSettings {

    ....

    @OneToOne(fetch = FetchType.LAZY)  // <-------------
    private User user;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring数据hibernate延迟加载 - spring data hibernate lazy loading Spring Boot + Hibernate + Thymeleaf 延迟加载异常 - Spring Boot + Hibernate + Thymeleaf Lazy loading exception "ManyToOne延迟加载不起作用Spring启动" - ManyToOne Lazy loading not working Spring boot JPA 延迟加载在 Spring 引导中不起作用 - JPA Lazy loading is not working in Spring boot 使用Spring数据和HIbernate JPA进行延迟加载 - Lazy Loading with Spring Data and HIbernate JPA Spring Data JPA和Hibernate的延迟加载异常 - Lazy loading exception with Spring Data JPA and Hibernate Hibernate 延迟加载不适用于 Spring 启动 =&gt; 无法延迟初始化角色集合无法初始化代理 - 没有 Session - Hibernate Lazy loading not working with Spring Boot => failed to lazily initialize a collection of role could not initialize proxy - no Session 从 JpaRepository 检索 object 后,延迟加载在简单的 Hibernate/Spring 启动示例中不起作用 - Lazy loading doesn't work in simple Hibernate/Spring boot example, after retrieving object from JpaRepository 当杰克逊与弹簧靴一起使用时,如何防止休眠延迟加载? - How to prevent hibernate lazy loading when using jackson with spring boot? Spring 引导测试上下文未加载:Flyway 和 Spring 引导数据/休眠 - Spring Boot test context not loading: Flyway and Spring Boot Data/Hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM