In my current Spring Boot REST project, I have two objects ( Bookand BookSummary) provided PagingAndSortingRepository. The object is Bookas 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 BookSummaryas 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 PagingAndSortingRepositoryas follows:
@Repository
public interface BookRepository extends PagingAndSortingRepository<Book, UUID>
{
Page<Book> findAll(Pageable pageable);
}
The object is BooksRestControlleras 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 PagingAndSortingRepositoryand BooksController, I would suggest that the REST service will provide a collection of objects Bookthrough 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-9a214cf093aecontains 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?