LazyInitializationException after return updated entity

huangapple 未分类评论49阅读模式
英文:

LazyInitializationException after return updated entity

问题

我有一个带有两个关联关系的模型

@Entity
@Table(name = "data_model")
public class DataModel {

    @Id
    @GeneratedValue
    @Column(name = "model_id")
    private Integer id;

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

    @OneToMany(mappedBy = "dataModel", cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
    private List<OutputField> outputFields;

    @OneToMany(mappedBy = "dataModel", cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
    private List<Query> queries;

    //一些其他字段
}

我使用 Spring Data JPA 并且想要更新实体。我编写了一个简单的服务:

@Service
public class DataModelService {

@Autowired
private DataModelRepository dataModelRepository;

@Transactional
public DataModel createOrUpdate(DataModel dataModel) {
   return dataModelRepository.save(dataModel);
}

//其他方法

}


我编写了一个简单的测试:

```java
public class DataModelServiceTest {
    @Autowired
    private DataModelService dataModelService;

    @Test
    void shouldUpdateDataModel() {
       DataModel dataModelBeforeUpdate = dataModelService.getById(1);
       dataModelBeforeUpdate.getQueries().get(0).setSqlQuery("SELECT 1");
       DataModel updatedModel = dataModelService.createOrUpdate(dataModelBeforeUpdate);
       assertThat(updatedModel.getQueries(), notNullValue());
    }
}

但是,当我尝试调用方法 getQieries() 时,我遇到错误:

无法评估表达式。方法引发了 'org.hibernate.LazyInitializationException' 异常。

在调试中,我看到:
LazyInitializationException after return updated entity

问题:

  1. 为什么会出现此错误,如何修复?如何使 Hibernate 在更新后返回所有链接?
  2. 为什么 outputFields 字段填充正确,而 queries 字段没有被填充?

<details>
<summary>英文:</summary>

I have some model with two relations:

```java
@Entity
@Table(name = &quot;data_model&quot;)
public class DataModel {

    @Id
    @GeneratedValue
    @Column(name = &quot;model_id&quot;)
    private Integer id;

    @Column(name = &quot;name&quot;)
    private String name;

    @OneToMany(mappedBy = &quot;dataModel&quot;, cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
    private List&lt;OutputField&gt; outputFields;

    @OneToMany(mappedBy = &quot;dataModel&quot;, cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
    private List&lt;Query&gt; queries;

    //some another fields
}

I use Spring Data JPA and I want to update entity. I write simple service:

@Service
public class DataModelService {

    @Autowired
    private DataModelRepository dataModelRepository;

    @Transactional
    public DataModel createOrUpdate(DataModel dataModel) {
       return dataModelRepository.save(dataModel);
    }

    //another methods
}

I write simple test:

public class DataModelServiceTest {
    @Autowired
    private DataModelService dataModelService;

    @Test
    void shouldUpdateDataModel() {
       DataModel dataModelBeforeUpdate = dataModelService.getById(1);
       dataModelBeforeUpdate.getQueries().get(0).setSqlQuery(&quot;SELECT 1&quot;);
       DataModel updatedModel = dataModelService.createOrUpdate(dataModelBeforeUpdate);
       assertThat(updatedModel.getQueries(), notNullValue());
    }
}

But, I get error, when I try to call method getQieries():

Unable to evaluate the expression Method threw &#39;org.hibernate.LazyInitializationException&#39; exception.

In debug I see:
LazyInitializationException after return updated entity

Questions:

  1. Why does this error occur and how can I fix it? How do I make hibernate return all links after an update?
  2. Why is the outputFields field filled in correctly, but the queries field is not?

答案1

得分: -1

因为您正在尝试在事务外部初始化集合,所以会出现这个问题。要解决这个问题,请将 @DataJpaTest@RunWith(SpringRunner.class) 注解添加到您的测试类中。默认情况下,数据 JPA 测试是事务性的。

更多详情请参考此处

英文:

It happens because you are trying to initialize collection outside a transaction. To fix this add @DataJpaTest and @RunWith(SpringRunner.class) annotations to your test class. By default, data JPA tests are transactional.

Refer here for more details.

huangapple
  • 本文由 发表于 2020年5月4日 00:03:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/61577561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定