Spring启动数据JPA:休眠会话问题 [英] Spring Boot Data JPA: Hibernate Session issue

查看:249
本文介绍了Spring启动数据JPA:休眠会话问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于Spring Boot的Web应用程序。我严重依赖 @ComponentScan @EnableAutoConfiguration 没有明确的XML 配置。



我有以下问题。我有一个名为 UserSettings JPA - 未注明的实体类:
$ b

  @Entity public class UserSettings {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
私人长ID;

@OneToMany(cascade = CascadeType.ALL)
private Set< Preference>优先; //'Preference'是另一个@Entity类

public UserSettings(){
this.preferences = new HashSet< Preference>();
}

//一些更原始的属性,Getters,Setters ...
}

我遵循教程和创建了一个扩展 JpaRepository< UserSettings,Long> 的存储库接口。另外,我有一个 UserManager bean:

  @Component public class SettingsManager {

@Autowired
UserSettingsRepository settingsRepository;

@PostConstruct
protected void init(){
//'findGlobalSettings'是一个简单的自定义HQL查询
UserSettings globalSettings = this.settingsRepository.findGlobalSettings();
if(globalSettings == null){
globalSettings = new UserSettings();
this.settingsRepository.saveAndFlush(globalSettings);






在代码后面,我加载了 UserSettings 这里创建的对象,再次使用 findGlobalSetttings 查询。



问题是:每当我尝试访问设置对象的 @OneToMany 属性时,以下例外:


org.hibernate.LazyInitializationException:未能延迟初始化角色org.example.UserSettings.preferences的集合,无法初始化代理 - 没有会话


据我所知,每个HTTP会话都有自己的Hibernate会话,如这个问题,但这并不适用于我的情况(目前我正在测试同样的HTTP会话),这就是为什么我不知道这个异常来自何处。



我做错了什么,我该如何避免这个错误?

解决方案

如果您希望能够访问事务外的映射实体(您似乎要做),你需要将它标记为渴望加入。即

  @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)


I'm developing a Spring Boot based web application. I heavily rely on @ComponentScan and @EnableAutoConfiguration and no explicit XML configuration in place.

I have the following problem. I have a JPA-Annotated Entity class called UserSettings:

@Entity public class UserSettings {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @OneToMany(cascade = CascadeType.ALL)
    private Set<Preference> preferences; // 'Preference' is another @Entity class

    public UserSettings() {
         this.preferences = new HashSet<Preference>();
    }

// some more primitive properties, Getters, Setters...
}

I followed this tutorial and created a repository interface that extends JpaRepository<UserSettings,Long>.

Furthermore, I have a UserManager bean:

@Component public class SettingsManager {

@Autowired
UserSettingsRepository settingsRepository;

@PostConstruct
protected void init() {
    // 'findGlobalSettings' is a simple custom HQL query 
    UserSettings globalSettings = this.settingsRepository.findGlobalSettings();
    if (globalSettings == null) {
        globalSettings = new UserSettings();
        this.settingsRepository.saveAndFlush(globalSettings);
    }
}

Later in the code, I load the UserSettings object created here, again with the findGlobalSetttings query.

The problem is: Every time I try to access the @OneToMany attribute of the settings object, I get the following exception:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role org.example.UserSettings.preferences, could not initialize proxy - no Session

I understand that each HTTP Session has its own Hibernate Session, as described in the accepted answer of this question, but that does not apply in my case (currently I'm testing this within the same HTTP Session), which is why I have no idea where this exception comes from.

What am I doing wrong and how can I achieve circumvent the error?

解决方案

If you want to be able to access mapped entities outside the transaction (which you seem to be doing), you need to flag it as an "eager" join. i.e.

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)

这篇关于Spring启动数据JPA:休眠会话问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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