Automatic mapping of Hibernate name objects

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?

+4
source share
1 answer

, Hibernate , , :

@Entity(name = "Book")

@Entity Hibernate .

: Book BookSummary. , Spring, . :

@RestController
@RequestMapping("/books")
public class BooksRestController
{
    private final BookRepository bookRepository;
    private final BookSummaryRepository bookSummaryRepository;

    @Autowired
    public BooksRestController(BookRepository bookRepository, BookSummaryRepository bookSummaryRepository)
    {
        this.bookRepository = bookRepository;
        this.bookSummaryRepository = bookSummaryRepository;
    }

    @GetMapping("/{uuid}")
    public BookSummary read(@PathVariable UUID uuid)
    {
        return bookSummaryRepository.findOne(uuid);
    }

    @GetMapping
    public Page<Book> read(Pageable pageable)
    {
        return bookRepository.findAll(pageable);
    }
}
+2

Source: https://habr.com/ru/post/1692270/


All Articles