0

I have a Parent Child relation like Library and Books,

public class Library {    
  @OneToMany()
  private List<Books> books;    
}

public class Book {    
 @ManytoOne()
 private  Library;
}

public interface LibraryRepository extends PagingAndSortingRepository<Library, Long> {
}

On querying the library repository I am getting the books also along with library information. I want to not list the books in the query , just get all the libraries..

Biswajit_86
  • 3,661
  • 2
  • 22
  • 36
sudhir
  • 53
  • 1
  • 4

1 Answers1

0

Use fetch = FetchType.LAZY:

public class Library {

@OneToMany(fetch = FetchType.LAZY)
private List<Books> books;

}

and also use DTO to return result.DTO is using for what do you want to show client.You can change your dto class property names different from table names in db for security reason.Your DTO class should be like this:

 public class LibraryDTO {
 //private properties belongs to Library
 //getters and setters 
   LibraryDTO(Library lib){
      this.id = lib.getId();
      //etc
   }
}

Initialize your dto's in controller class:

    List<Library> libraries = serviceLibrary.getLibraries();

    List<LibraryDTO> libraryDTOs= new ArrayList<LibraryDTO>();
    for (Library library: libraries) {
       libraryDTOs.append(new LibraryDTO(library));
    }
Sarkhan
  • 1,281
  • 1
  • 11
  • 33