Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. package files.Spring01hibernate.model;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import javax.persistence.CascadeType;
  8. import javax.persistence.Column;
  9. import javax.persistence.Entity;
  10. import javax.persistence.FetchType;
  11. import javax.persistence.GeneratedValue;
  12. import javax.persistence.GenerationType;
  13. import javax.persistence.Id;
  14. import javax.persistence.ManyToMany;
  15. import javax.persistence.ManyToOne;
  16. import javax.persistence.Table;
  17.  
  18. @Entity
  19. @Table(name = "books")
  20. public class Book {
  21.  
  22. @Id
  23. @GeneratedValue(strategy = GenerationType.IDENTITY)
  24. private Long id;
  25. private String title;
  26.  
  27. @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
  28. private List<Author> authors = new ArrayList<>();
  29.  
  30. @Column(precision = 4, scale = 2)
  31. private BigDecimal rating;
  32.  
  33. @ManyToOne
  34. private Publisher publisher;
  35.  
  36. @Column(columnDefinition = "TEXT")
  37. private String description;
  38.  
  39. ////////////////////////////////////////////////////////////////////////
  40.  
  41. public long getId() {
  42. return id;
  43. }
  44.  
  45. public String getTitle() {
  46. return title;
  47. }
  48.  
  49. public List<Author> getAuthor() {
  50. return authors;
  51. }
  52.  
  53. public BigDecimal getRating() {
  54. return rating;
  55. }
  56.  
  57. public Publisher getPublisher() {
  58. return publisher;
  59. }
  60.  
  61. public String getDescription() {
  62. return description;
  63. }
  64.  
  65. public void setId(long id) {
  66. this.id = id;
  67. }
  68.  
  69. public void setTitle(String title) {
  70. this.title = title;
  71. }
  72.  
  73. public void setAuthor(Author author) {
  74. authors.add(author);
  75. }
  76.  
  77. public void setRating(BigDecimal rating) {
  78. this.rating = rating;
  79. }
  80.  
  81. public void setPublisher(Publisher publisher) {
  82. this.publisher = publisher;
  83. }
  84.  
  85. public void setDescription(String description) {
  86. this.description = description;
  87. }
  88.  
  89. }
  90.  
  91. ///////////////////////////////////////////////////////////////////////
  92.  
  93. package files.Spring01hibernate.model;
  94.  
  95. import java.util.ArrayList;
  96. import java.util.List;
  97.  
  98. import javax.persistence.CascadeType;
  99. import javax.persistence.Entity;
  100. import javax.persistence.FetchType;
  101. import javax.persistence.GeneratedValue;
  102. import javax.persistence.GenerationType;
  103. import javax.persistence.Id;
  104. import javax.persistence.ManyToMany;
  105. import javax.persistence.Table;
  106.  
  107. import com.fasterxml.jackson.annotation.JsonIgnore;
  108.  
  109. @Entity
  110. @Table(name = "authors")
  111. public class Author {
  112.  
  113. @Id
  114. @GeneratedValue(strategy = GenerationType.IDENTITY)
  115. private Long id;
  116. private String firstName;
  117. private String lastName;
  118.  
  119. @JsonIgnore
  120. @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
  121. private List<Book> books = new ArrayList<>();
  122.  
  123. /////////////////////////////////////////////////////////////
  124.  
  125. public Long getId() {
  126. return id;
  127. }
  128. public String getFirstName() {
  129. return firstName;
  130. }
  131. public String getLastName() {
  132. return lastName;
  133. }
  134. public void setId(Long id) {
  135. this.id = id;
  136. }
  137. public void setFirstName(String firstName) {
  138. this.firstName = firstName;
  139. }
  140. public void setLastName(String lastName) {
  141. this.lastName = lastName;
  142. }
  143. public List<Book> getBooks() {
  144. return books;
  145. }
  146. public void setBooks(Book book) {
  147. books.add(book);
  148. }
  149.  
  150. }
  151.  
  152. ////////////////////////////////////////////////////////////////
  153.  
  154.  
  155. package files.Spring01hibernate.controller;
  156.  
  157. import java.math.BigDecimal;
  158. import java.util.List;
  159.  
  160. import org.springframework.beans.factory.annotation.Autowired;
  161. import org.springframework.web.bind.annotation.PathVariable;
  162. import org.springframework.web.bind.annotation.RequestMapping;
  163. import org.springframework.web.bind.annotation.RequestMethod;
  164. import org.springframework.web.bind.annotation.RequestParam;
  165. import org.springframework.web.bind.annotation.RestController;
  166.  
  167. import files.Spring01hibernate.dao.BookDao;
  168. import files.Spring01hibernate.dao.PublisherDao;
  169. import files.Spring01hibernate.model.Author;
  170. import files.Spring01hibernate.model.Book;
  171. import files.Spring01hibernate.model.Publisher;
  172.  
  173. //@Controller
  174. @RestController
  175. @RequestMapping("/books")
  176. public class BookController {
  177.  
  178. @Autowired
  179. private BookDao bookDao;
  180.  
  181. @Autowired
  182. private PublisherDao publisherDao;
  183.  
  184. @RequestMapping(method = RequestMethod.POST)
  185. //public String addBook(){
  186. public Book addBook(){
  187. Book book = new Book();
  188. book.setTitle("Talizman");
  189.  
  190. Author author1 = new Author();
  191. author1.setFirstName("Stephen");
  192. author1.setLastName("King");
  193.  
  194. Author author2 = new Author();
  195. author2.setFirstName("Peter");
  196. author2.setLastName("Straub");
  197.  
  198. book.setAuthor(author1);
  199. book.setAuthor(author2);
  200. book.setDescription("Szukają talizmanu.");
  201.  
  202. //Publisher publisher = new Publisher();
  203. //publisher.setName("Helion");
  204. Publisher publisher = publisherDao.find(2L);
  205.  
  206. book.setPublisher(publisher);
  207.  
  208. book.setRating(BigDecimal.valueOf(6.55));
  209.  
  210. bookDao.save(book);
  211.  
  212. //return "Added book, #id = " + book.getId();
  213. return book;
  214. }
  215.  
  216. @RequestMapping(path = "/{id}", method = RequestMethod.GET)
  217. //@ResponseBody
  218. public Book getBook(@PathVariable Long id){
  219. Book book = bookDao.find(id);
  220.  
  221. //return "Loaded book, #id = " + book.getId();
  222. return book;
  223. }
  224.  
  225. @RequestMapping(path = "/{id}", method = RequestMethod.PUT)
  226. public Book updateBook(@PathVariable Long id){
  227. Book book = bookDao.find(id);
  228. book.setDescription("updated");
  229. bookDao.update(book);
  230. return book;
  231. }
  232.  
  233. @RequestMapping(path = "/{id}", method = RequestMethod.DELETE)
  234. public Book deleteBook(@PathVariable Long id){
  235. Book book = bookDao.find(id);
  236. bookDao.delete(book);
  237. return book;
  238. }
  239.  
  240. @RequestMapping(method = RequestMethod.GET)
  241. public List<Book> getAll(){
  242. List<Book> books = bookDao.findAll();
  243. return books;
  244. }
  245.  
  246. @RequestMapping(method = RequestMethod.GET, params = "rating")
  247. public List<Book> getBooksWithRating(@RequestParam BigDecimal rating){
  248. List<Book> books = bookDao.findAllWithRating(rating);
  249. return books;
  250. }
  251.  
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement