【发布时间】:2018-01-16 15:55:44
【问题描述】:
在我当前的 Spring Boot REST 示例项目中,我有两个实体(Book 和 BookSummary)均由 PagingAndSortingRepository 提供。 Book 实体如下所示:
@Entity(name = "Book")
public class Book
{
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID uuid;
private String title;
private String author;
private String publisher;
@Transient
private String type = "Book";
...[Getter & Setter]...
}
BookSummary 实体如下所示:
@Entity(name = "Book")
public class BookSummary
{
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID uuid;
private String title;
private String author;
@Transient
private String type = "BookSummary";
...[Getter & Setter]...
}
PagingAndSortingRepository 实体如下所示:
@Repository
public interface BookRepository extends PagingAndSortingRepository<Book, UUID>
{
Page<Book> findAll(Pageable pageable);
}
BooksRestController 实体如下所示:
@RestController
@RequestMapping("/books")
public class BooksRestController
{
@GetMapping("/{uuid}")
public Book read(@PathVariable UUID uuid)
{
return bookRepository.findOne(uuid);
}
@GetMapping
public Page<Book> read(Pageable pageable)
{
return bookRepository.findAll(pageable);
}
@Autowired
private BookRepository bookRepository;
}
关于PagingAndSortingRepository 和BooksController 实现,我假设REST 服务将通过/books 路由提供Book 实体的集合。但是该路由提供了BookSummary 实体的集合:
{
content: [
{
uuid: "41fb943e-fad4-11e7-8c3f-9a214cf093ae",
title: "Some Title",
author: "Some Guy",
type: "BookSummary"
},
...
]
}
books/41fb943e-fad4-11e7-8c3f-9a214cf093ae 路由却提供了Book 摘要(如预期的那样):
{
uuid: "41fb943e-fad4-11e7-8c3f-9a214cf093ae",
title: "Some Title",
author: "Some Guy",
publisher: "stackoverflow.com"
type: "Book"
}
有人可以帮助我理解 Hibernate 的以下行为吗?
【问题讨论】:
标签: hibernate jpa spring-boot spring-data-jpa