我有像图书馆和图书这样的父子关系,
public class Library {
@OneToMany()
private List<Books> books;
}
public class Book {
@ManytoOne()
private Library;
}
public interface LibraryRepository extends PagingAndSortingRepository<Library, Long> {
}
在查询库存储库时,我还会获取书籍以及库信息。我想不列出查询中的书籍,只需获取所有库..
答案 0 :(得分:0)
使用fetch = FetchType.LAZY:
public class Library {
@OneToMany(fetch = FetchType.LAZY)
private List<Books> books;
}
并且还使用DTO返回result.DTO用于显示客户端的内容。出于安全原因,您可以更改与db中的表名不同的dto类属性名。您的DTO类应该是这样的:< / p>
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));
}