3

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.

Nurjan
  • 5,532
  • 5
  • 32
  • 50
iqueqiorio
  • 1,119
  • 2
  • 32
  • 76

2 Answers2

3

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. one solution is to define a Dto class for the Parent class that does not include Child, for example

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

then in rest controller return 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

fatemeakbari
  • 91
  • 1
  • 5
  • 1
    **MapStruct** is also very efficient, check this answer that prevent the same problem : https://stackoverflow.com/a/65391112/2641426 – DependencyHell Dec 21 '20 at 10:31
0

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

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. Also note that @OneToMany and @ManyToMany associations are defaulted to LAZY loading; and @OneToOne and @ManyToOne are defaulted to EAGER loading. 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. An example usage will look like this: @OneToMany( mappedBy = "category", fetch = FetchType.LAZY ) private Set products; Another attribute parallel to "FetchType.LAZY" is "FetchType.EAGER" which is just opposite to LAZY i.e. it will load association entity as well when owner entity is fetched first time. 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 intercepts calls to the entity by substituting a proxy for it derived from the entity’s class. 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.

0gam
  • 1,168
  • 8
  • 19