Suppose I have to save an entity, in this case, Book. I have the next code:
@RestController
@RequestMapping("books")
public class BookController {
@Inject
BookRepository bookRepository;
@PostMapping
public Book saveBook(@RequestBody Book book) {
return bookRepository.save(book);
}
}
My entity Book is a persistence entity:
@Entity(name = "BOOK")
public class Book{
@Id
@Column(name = "book_id")
private Integer id;
@Column(name = "title")
private String title;
(get/sets...)
}
The question is: is a bad practice use my persistence entity in @RequestBody
of the controller layer? Or should I create a book DTO and map it to my persistence class in a service layer? What is better and why?