简体   繁体   English

如何在 Spring Boot JPA 的 OneToMany 中为孩子获取 Null 值

[英]How get Null value for child in OneToMany in Spring Boot JPA

I need to get simple List of Parent objects with null values for childs.我需要为孩子获取具有空值的简单父对象列表。 But when i use findAll() method and then try to get child object i get LazyInitializationException: failed to lazily initialize a collection of role: ... could not initialize proxy - no Session I see explanation of this problem (about Lazy/Eager, JOIN FETCH), but i need to get null for child objects without query for childs.但是当我使用 findAll() 方法然后尝试获取子对象时,我得到 LazyInitializationException: failed to lazyly initialize a collection of role: ... could not initialize proxy - no Session 我看到了这个问题的解释(关于 Lazy/Eager, JOIN FETCH),但我需要为子对象获取空值而不查询子对象。

@Entity
public class Parent {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "parent")
    Set<Child> childs;

@Entity
public class Child {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    Parent parent;

@Service
    public class ParentService {

    @Autowired
    ParentRepository parentRepository;

    public List<Course> getParentList() {
        return parentRepository.findAll();
    }

In hibernate i get correct query:在休眠中,我得到正确的查询:

select parent0_.id as id1_0_ from parent parent0_

Before test i add few parent entities in DB, result not empty, and on this test i get error LazyInitializationException在测试之前我在数据库中添加了几个父实体,结果不为空,在这个测试中我得到错误 LazyInitializationException

@Test
    public void checkParentListFormat() {
        List<Parent> parentList = parentService.getParentList();
        Assertions.assertThat(parentList.get(0).getChilds()).isNull();
    }


I read about DTO, but is it possible to get simple Entity with null values?我读过 DTO,但是否有可能获得具有空值的简单实体? Thanks谢谢

I think you need to put your lazy initialization in the parent annotation.我认为您需要将延迟初始化放在父注释中。

@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
Set<Child> childs;

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

相关问题 春季启动OnetoMany与JPA - Spring boot OnetoMany with JPA @OneToMany在spring数据jpa中给出了空值 - @OneToMany is giving null value in spring data jpa 如何实现一对多单向关系(Spring boot + JPA) - How to OneToMany unidirectional relationship (Spring boot + JPA) OneToMany 返回 null 响应来自目标实体的 JoinColumn,即使值已插入数据库 - Spring 启动 JPA - OneToMany returns null in response on JoinColumn from target Entity even though value was inserted in database - Spring boot JPA 如何在 spring 引导中由 @OneToMany 注释映射的列上使用 JPA findBy - How to use JPA findBy on column mapped by @OneToMany annotation in spring boot 如何在 Spring 启动 JPA 中加入 OneToMany 与复杂的 Where 条件? - How to join OneToMany in Spring boot JPA with a complex Where condition? 使用OneToMany的Spring Boot JPA自定义查询 - Spring Boot JPA custom Query with OneToMany spring-boot-starter-data-jpa @OneToMany 在更新时不会插入新子项 - spring-boot-starter-data-jpa @OneToMany does not insert new child when it's an update 软删除:在Spring Boot JPA Hibernate中删除@OneToMany关系中的父实体后,不会删除子实体 - Soft Delete : Child Entity not being deleted after Deleting parent Entity in @OneToMany relation in Spring Boot JPA Hibernate Spring-子对象上没有setParent的JPA OneToMany - Spring - JPA OneToMany without setParent on Child object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM