2015-05-04 119 views
0

我有一個圖書館和冊的親子關係,春庫和延遲加載

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

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

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

在查詢庫的儲存庫,我得到的書籍也隨之庫信息。我想列表中查詢書,只得到所有的庫..

+0

查看這裏取得的類型 http://stackoverflow.com/a/18605401/1844148 – Dileep

回答

0

使用讀取= FetchType.LAZY:

public class Library { 

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

} 

,還可以使用DTO result.DTO被使用返回你有什麼想顯示client.You可以改變從表名不同的DTO類屬性名以dB爲安全reason.Your DTO類應該是這樣的:

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

初始化您的DTO在控制器類:

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

    List<LibraryDTO> libraryDTOs= new ArrayList<LibraryDTO>(); 
    for (Library library: libraries) { 
     libraryDTOs.append(new LibraryDTO(library)); 
    }