Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

specifying whether or not to lazily load with spring data

specifying whether or not to lazily load with spring data  using -'java,spring,hibernate,jpa,spring-data'

I have a lazy fetch type collection in an entity. And I am using Spring Data (JpaRepository) for accessing the entities.

public class Parent{
private Long id;

    @OneToMany(mappedBy = "parentId", fetch = FetchType.LAZY)
    private Set<Child> children;

I want two functions in service class and current implementation are as following:

"children" should be null when fetching parent

public Parent getParent(Long parentId){
    return repo.findOne(parentId);

"children" should be filled when fetching parent:

 public Parent getParentWithChildren(Long parentId){
     Parent p = repo.findOne(parentId);
     return p;

When returning "Parent" entity from a RestController, following exception is thrown:

public Parent getParent(@PathVariable("parentId") Long id)
    Parent p= parentService.getParent(id);//ok till here
    return p;//error thrown when converting to JSON

  Could not write content: failed to lazily initialize a collection of
  role: com.entity.Parent.children, could not initialize proxy - no
  Session (through reference chain: com.entity.Parent["children"]);
  nested exception is
  com.fasterxml.jackson.databind.JsonMappingException: failed to lazily
  initialize a collection of role: com.entity.Parent.children, could not
  initialize proxy - no Session (through reference chain:


asked Oct 6, 2015 by patelnikul321
0 votes

Related Hot Questions

Government Jobs Opening