简体   繁体   English

JPA - 只得到父母,而不是孩子

[英]JPA - Only get parent, not children

I have a parent like this:我有这样的父母:

@Entity
@Table(name="parent")
public class Parent {

    private List<Child> childs;
    private List<AnotherChild> anotherChilds;

    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    public List<Child> getChilds() {
        return childs;
    }

    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    public List<AnotherChild> getAntoherChilds() {
        return anotherChilds;
    }

    //Getters and Setters ommited
}

And two children like this还有这样的两个孩子

@Entity
@Table(name="child")
public class Child {

    private Parent parent;

    @ManyToOne
    @JoinColumn(name = "column_name")
    public Parent getParent() {
        return patern;
    }
}

@Entity
@Table(name="another_child")
public class AnotherChild {

    private Parent parent;

    @ManyToOne
    @JoinColumn(name = "column_name")
    public Parent getParent() {
        return patern;
    }
}

I have a named query which gets all the Parents, but this is also loading all the children?我有一个命名查询,它获取所有的父母,但这也正在加载所有的孩子? How can I stop the children for automatically loading?如何阻止孩子自动加载?

Thanks.谢谢。

I know my answer is late, but it may help others because I had the same problem for a while, based this answer if you use rest API, Spring calls Jackson to return Parent object and Jackson calls getChild, for this reason, parent's child loaded.我知道我的回答晚了,但它可能会帮助其他人,因为我有一段时间遇到同样的问题,如果您使用 rest API,则基于此答案,Spring 调用 Jackson 返回 Parent 对象,Jackson 调用 getChild,因此,加载了父母的孩子. one solution is to define a Dto class for the Parent class that does not include Child, for example一种解决方案是为不包含 Child 的 Parent 类定义一个 Dto 类,例如

public class ParentResponseDto{
    private Long id
    private String name; 
    //and your desired attribute that you want to load
}

then in rest controller return ParenResponseDto然后在休息控制器中返回 ParenResponseDto

@GetMapping
public ParenResponseDto getParent(Long id){
    Parent p = repository.findById(id);
    ParenResponseDto dto = mapParentToParenResponseDto();
    return dto;
}

you can use ModelMapper to map classes, this is a great module您可以使用ModelMapper来映射类,这是一个很棒的模块

Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed.延迟加载是计算机编程中常用的一种设计模式,用于将对象的初始化推迟到需要的时候。 It can contribute to efficiency in the program's operation if properly and appropriately used.如果使用得当和适当,它可以提高程序运行的效率。 The opposite of lazy loading is eager loading .延迟加载的反面是预先加载

How to Enable Lazy Loading in Hibernate如何在 Hibernate 中启用延迟加载

Before moving further, it is important to recap the default behavior of lazy loading in case of using hibernate mappings vs annotations.在继续之前,重要的是回顾一下延迟加载的默认行为,以防使用休眠映射与注释。 The default behavior is to load 'property values eagerly' and to load 'collections lazily'.默认行为是急切加载“属性值”和“延迟加载”。 Contrary to what you might remember if you have used plain Hibernate 2 (mapping files) before, where all references (including collections) are loaded eagerly by default.与之前使用普通 Hibernate 2(映射文件)时可能记得的相反,默认情况下,所有引用(包括集合)都是急切加载的。 Also note that @OneToMany and @ManyToMany associations are defaulted to LAZY loading;还要注意@OneToMany 和@ManyToMany 关联默认为延迟加载; and @OneToOne and @ManyToOne are defaulted to EAGER loading. @OneToOne 和 @ManyToOne 默认为 EAGER 加载。 This is important to remember to avoid any pitfall in future.记住这一点很重要,以避免将来出现任何陷阱。 To enable lazy loading explicitly you must use "fetch = FetchType.LAZY" on a association which you want to lazy load when you are using hibernate annotations.要显式启用延迟加载,您必须在使用休眠注释时要延迟加载的关联上使用“fetch = FetchType.LAZY”。 An example usage will look like this: @OneToMany( mappedBy = "category", fetch = FetchType.LAZY ) private Set products;示例用法如下所示:@OneToMany(mappedBy = "category", fetch = FetchType.LAZY) private Set products; Another attribute parallel to "FetchType.LAZY" is "FetchType.EAGER" which is just opposite to LAZY ie it will load association entity as well when owner entity is fetched first time.与“FetchType.LAZY”平行的另一个属性是“FetchType.EAGER”,它与 LAZY 正好相反,即当第一次获取所有者实体时,它也会加载关联实体。 How Lazy Loading Works in Hibernate The simplest way that Hibernate can apply lazy load behavior upon your entities and associations is by providing a proxy implementation of them. Hibernate 中延迟加载的工作原理 Hibernate 可以对实体和关联应用延迟加载行为的最简单方法是提供它们的代理实现。 Hibernate intercepts calls to the entity by substituting a proxy for it derived from the entity's class. Hibernate 通过替换从实体类派生的代理来拦截对实体的调用。 Where the requested information is missing, it will be loaded from the database before control is ceded to the parent entity's implementation.如果缺少所请求的信息,它将在控制权交给父实体的实现之前从数据库中加载。 Please note that when the association is represented as a collection class, then a wrapper (essentially a proxy for the collection, rather than for the entities that it contains) is created and substituted for the original collection.请注意,当关联表示为集合类时,会创建一个包装器(本质上是集合的代理,而不是其包含的实体)并替换原始集合。 When you access this collection proxy then what you get inside returned proxy collection are not proxy entities;当您访问这个集合代理时,您在返回的代理集合中得到的不是代理实体; rather they are actual entities.相反,它们是实际的实体。 You need not to put much pressure on understanding this concept because on runtime it hardly matters.您不必对理解这个概念施加太大压力,因为在运行时它几乎不重要。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM