In my current Spring Boot REST project, I have two objects ( Book
and BookSummary
) provided PagingAndSortingRepository
. The object is Book
as follows:
@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]...
}
The object is BookSummary
as follows:
@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]...
}
The object is PagingAndSortingRepository
as follows:
@Repository
public interface BookRepository extends PagingAndSortingRepository<Book, UUID>
{
Page<Book> findAll(Pageable pageable);
}
The object is BooksRestController
as follows:
@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;
}
As for the implementation of PagingAndSortingRepository
and BooksController
, I would suggest that the REST service will provide a collection of objects Book
through the route /books
. However, the route provides a collection of objects BookSummary
:
{
content: [
{
uuid: "41fb943e-fad4-11e7-8c3f-9a214cf093ae",
title: "Some Title",
author: "Some Guy",
type: "BookSummary"
},
...
]
}
However, the route books/41fb943e-fad4-11e7-8c3f-9a214cf093ae
contains a summary Book
(as expected):
{
uuid: "41fb943e-fad4-11e7-8c3f-9a214cf093ae",
title: "Some Title",
author: "Some Guy",
publisher: "stackoverflow.com"
type: "Book"
}
Can someone help me understand the following Hibernate behavior?