Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package files.Spring01hibernate.model;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.List;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.ManyToMany;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- @Entity
- @Table(name = "books")
- public class Book {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
- private String title;
- @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
- private List<Author> authors = new ArrayList<>();
- @Column(precision = 4, scale = 2)
- private BigDecimal rating;
- @ManyToOne
- private Publisher publisher;
- @Column(columnDefinition = "TEXT")
- private String description;
- ////////////////////////////////////////////////////////////////////////
- public long getId() {
- return id;
- }
- public String getTitle() {
- return title;
- }
- public List<Author> getAuthor() {
- return authors;
- }
- public BigDecimal getRating() {
- return rating;
- }
- public Publisher getPublisher() {
- return publisher;
- }
- public String getDescription() {
- return description;
- }
- public void setId(long id) {
- this.id = id;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public void setAuthor(Author author) {
- authors.add(author);
- }
- public void setRating(BigDecimal rating) {
- this.rating = rating;
- }
- public void setPublisher(Publisher publisher) {
- this.publisher = publisher;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- }
- ///////////////////////////////////////////////////////////////////////
- package files.Spring01hibernate.model;
- import java.util.ArrayList;
- import java.util.List;
- import javax.persistence.CascadeType;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.ManyToMany;
- import javax.persistence.Table;
- import com.fasterxml.jackson.annotation.JsonIgnore;
- @Entity
- @Table(name = "authors")
- public class Author {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
- private String firstName;
- private String lastName;
- @JsonIgnore
- @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
- private List<Book> books = new ArrayList<>();
- /////////////////////////////////////////////////////////////
- public Long getId() {
- return id;
- }
- public String getFirstName() {
- return firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public List<Book> getBooks() {
- return books;
- }
- public void setBooks(Book book) {
- books.add(book);
- }
- }
- ////////////////////////////////////////////////////////////////
- package files.Spring01hibernate.controller;
- import java.math.BigDecimal;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import files.Spring01hibernate.dao.BookDao;
- import files.Spring01hibernate.dao.PublisherDao;
- import files.Spring01hibernate.model.Author;
- import files.Spring01hibernate.model.Book;
- import files.Spring01hibernate.model.Publisher;
- //@Controller
- @RestController
- @RequestMapping("/books")
- public class BookController {
- @Autowired
- private BookDao bookDao;
- @Autowired
- private PublisherDao publisherDao;
- @RequestMapping(method = RequestMethod.POST)
- //public String addBook(){
- public Book addBook(){
- Book book = new Book();
- book.setTitle("Talizman");
- Author author1 = new Author();
- author1.setFirstName("Stephen");
- author1.setLastName("King");
- Author author2 = new Author();
- author2.setFirstName("Peter");
- author2.setLastName("Straub");
- book.setAuthor(author1);
- book.setAuthor(author2);
- book.setDescription("Szukają talizmanu.");
- //Publisher publisher = new Publisher();
- //publisher.setName("Helion");
- Publisher publisher = publisherDao.find(2L);
- book.setPublisher(publisher);
- book.setRating(BigDecimal.valueOf(6.55));
- bookDao.save(book);
- //return "Added book, #id = " + book.getId();
- return book;
- }
- @RequestMapping(path = "/{id}", method = RequestMethod.GET)
- //@ResponseBody
- public Book getBook(@PathVariable Long id){
- Book book = bookDao.find(id);
- //return "Loaded book, #id = " + book.getId();
- return book;
- }
- @RequestMapping(path = "/{id}", method = RequestMethod.PUT)
- public Book updateBook(@PathVariable Long id){
- Book book = bookDao.find(id);
- book.setDescription("updated");
- bookDao.update(book);
- return book;
- }
- @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
- public Book deleteBook(@PathVariable Long id){
- Book book = bookDao.find(id);
- bookDao.delete(book);
- return book;
- }
- @RequestMapping(method = RequestMethod.GET)
- public List<Book> getAll(){
- List<Book> books = bookDao.findAll();
- return books;
- }
- @RequestMapping(method = RequestMethod.GET, params = "rating")
- public List<Book> getBooksWithRating(@RequestParam BigDecimal rating){
- List<Book> books = bookDao.findAllWithRating(rating);
- return books;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement