Guest User

Untitled

a guest
Feb 14th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.06 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project version="4">
  3. <component name="DataSourceManagerImpl" format="xml" multifile-model="true">
  4. <data-source source="LOCAL" name="welldb" uuid="78793bdd-e26d-42e7-95d9-c2a44584c136">
  5. <driver-ref>mysql</driver-ref>
  6. <synchronize>true</synchronize>
  7. <jdbc-driver>com.mysql.jdbc.Driver</jdbc-driver>
  8. <jdbc-url>jdbc:mysql://localhost:3306/test</jdbc-url>
  9. <driver-properties>
  10. <property name="autoReconnect" value="true" />
  11. <property name="zeroDateTimeBehavior" value="convertToNull" />
  12. <property name="tinyInt1isBit" value="false" />
  13. <property name="characterEncoding" value="utf8" />
  14. <property name="characterSetResults" value="utf8" />
  15. <property name="yearIsDateType" value="false" />
  16. </driver-properties>
  17. </data-source>
  18. </component>
  19. </project>
  20.  
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <beans xmlns="http://www.springframework.org/schema/beans"
  23. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  24. xmlns:context="http://www.springframework.org/schema/context"
  25. xmlns:mvc="http://www.springframework.org/schema/mvc"
  26. xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
  27. xsi:schemaLocation="http://www.springframework.org/schema/beans
  28. http://www.springframework.org/schema/beans/spring-beans.xsd
  29. http://www.springframework.org/schema/context
  30. http://www.springframework.org/schema/context/spring-context.xsd
  31. http://www.springframework.org/schema/mvc
  32. http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  33.  
  34. <context:component-scan base-package = "well.controllers" />
  35.  
  36. <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
  37. <property name = "prefix" value = "/WEB-INF/jsp/" />
  38. <property name = "suffix" value = ".jsp" />
  39. </bean>
  40.  
  41. <bean id="sessionFactory" class="well.dao.BookSessionFactory"/>
  42.  
  43. <bean id="bookDao" class="well.dao.BookDaoImpl">
  44. <property name="sessionFactory" ref="sessionFactory"/>
  45. </bean>
  46.  
  47. <bean id="bookService" class="well.service.BookServiceImpl">
  48. <property name="bookDao" ref="bookDao"/>
  49. </bean>
  50.  
  51. <mvc:default-servlet-handler/>
  52. <mvc:annotation-driven/>
  53.  
  54. <tx:annotation-driven transaction-manager="transactionManager"/>
  55.  
  56. <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
  57. p:sessionFactory-ref="sessionFactory"/>
  58.  
  59.  
  60. </beans>
  61.  
  62. package well.model;
  63.  
  64.  
  65. import javax.persistence.*;
  66. import java.util.Objects;
  67.  
  68. @Entity
  69. @Table(name = "library")
  70. public class Book {
  71. private Integer id;
  72. private String title;
  73. private String description;
  74. private String author;
  75. private String isbn;
  76. private Integer printYear;
  77. private Boolean readAlready;
  78.  
  79. @Id
  80. @Column(name = "id")
  81. public int getId() {
  82. return id;
  83. }
  84.  
  85. public void setId(int id) {
  86. this.id = id;
  87. }
  88.  
  89. @Basic
  90. @Column(name = "title")
  91. public String getTitle() {
  92. return title;
  93. }
  94.  
  95. public void setTitle(String title) {
  96. this.title = title;
  97. }
  98.  
  99. @Basic
  100. @Column(name = "description")
  101. public String getDescription() {
  102. return description;
  103. }
  104.  
  105. public void setDescription(String description) {
  106. this.description = description;
  107. }
  108.  
  109. @Basic
  110. @Column(name = "author")
  111. public String getAuthor() {
  112. return author;
  113. }
  114.  
  115. public void setAuthor(String author) {
  116. this.author = author;
  117. }
  118.  
  119. @Basic
  120. @Column(name = "isbn")
  121. public String getIsbn() {
  122. return isbn;
  123. }
  124.  
  125. public void setIsbn(String isbn) {
  126. this.isbn = isbn;
  127. }
  128.  
  129. @Basic
  130. @Column(name = "printYear")
  131. public Integer getPrintYear() {
  132. return printYear;
  133. }
  134.  
  135. public void setPrintYear(Integer printYear) {
  136. this.printYear = printYear;
  137. }
  138.  
  139. @Basic
  140. @Column(name = "readAlready")
  141. public Boolean getReadAlready() {
  142. return readAlready;
  143. }
  144.  
  145. public void setReadAlready(Boolean readAlready) {
  146. this.readAlready = readAlready;
  147. }
  148.  
  149. @Override
  150. public boolean equals(Object o) {
  151. if (this == o) return true;
  152. if (o == null || getClass() != o.getClass()) return false;
  153. Book book = (Book) o;
  154. return id == book.id &&
  155. Objects.equals(title, book.title) &&
  156. Objects.equals(description, book.description) &&
  157. Objects.equals(author, book.author) &&
  158. Objects.equals(isbn, book.isbn) &&
  159. Objects.equals(printYear, book.printYear) &&
  160. Objects.equals(readAlready, book.readAlready);
  161. }
  162.  
  163. @Override
  164. public int hashCode() {
  165.  
  166. return Objects.hash(id, title, description, author, isbn, printYear, readAlready);
  167. }
  168.  
  169. @Override
  170. public String toString() {
  171. return "Book{" +
  172. "id=" + id +
  173. ", title='" + title + ''' +
  174. ", description='" + description + ''' +
  175. ", author='" + author + ''' +
  176. ", isbn='" + isbn + ''' +
  177. ", printYear=" + printYear +
  178. ", readAlready=" + readAlready +
  179. '}';
  180. }
  181. }
  182.  
  183. package well.service;
  184.  
  185. import org.springframework.stereotype.Service;
  186. import org.springframework.transaction.annotation.Transactional;
  187. import well.dao.BookDao;
  188. import well.model.Book;
  189.  
  190. import java.util.List;
  191.  
  192. @Service
  193. public class BookServiceImpl implements BookService {
  194.  
  195. private BookDao bookDao;
  196.  
  197. public void setBookDao(BookDao bookDao) {
  198. this.bookDao = bookDao;
  199. }
  200.  
  201. @Transactional
  202. public void addBook(Book book) {
  203. bookDao.addBook(book);
  204. }
  205. @Transactional
  206. public void updateBook(Book book) {
  207. bookDao.updateBook(book);
  208. }
  209. @Transactional
  210. public void removeBook(int id) {
  211. System.out.println("remove " + id);
  212. bookDao.removeBook(id);
  213. }
  214. @Transactional
  215. public Book getBookById(int id) {
  216. return bookDao.getBookById(id);
  217. }
  218. @Transactional
  219. public List<Book> getListBooks() {
  220. return bookDao.getListBooks();
  221. }
  222. }
  223.  
  224. package well.dao;
  225.  
  226. import org.hibernate.Session;
  227. import org.hibernate.SessionFactory;
  228. import org.hibernate.Transaction;
  229. import org.slf4j.Logger;
  230. import org.slf4j.LoggerFactory;
  231. import org.springframework.stereotype.Service;
  232. import well.model.Book;
  233.  
  234. import java.util.List;
  235.  
  236.  
  237. @Service
  238. public class BookDaoImpl implements BookDao {
  239.  
  240. private SessionFactory sessionFactory;
  241.  
  242. public void setSessionFactory(SessionFactory sessionFactory) {
  243. this.sessionFactory = sessionFactory;
  244. }
  245.  
  246. private static final Logger logger = LoggerFactory.getLogger(BookDaoImpl.class);
  247. // private static Session session = BookSessionFactory.getSession();
  248. private Session session = sessionFactory.getCurrentSession();
  249.  
  250. public void addBook(Book book) {
  251.  
  252. Transaction tx = session.beginTransaction();
  253. session.persist(book);
  254. tx.commit();
  255. logger.info("Книга: " + book + " успешно добавлена.");
  256. }
  257.  
  258.  
  259. public void updateBook(Book book) {
  260. Transaction tx = session.beginTransaction();
  261. session.merge(book);
  262. tx.commit();
  263. logger.info("Книга: " + book + " успешно обновлена.");
  264.  
  265. }
  266.  
  267.  
  268. public void removeBook(int id) {
  269. Book book = getBookById(id);
  270. logger.info("Книга: " + book);
  271. if (book != null) {
  272. Transaction tx = session.beginTransaction();
  273. session.delete(book);
  274. tx.commit();
  275.  
  276. logger.info("Книга: " + book + " успешно удалена.");
  277. }
  278. }
  279.  
  280.  
  281. public Book getBookById(int id) {
  282. Transaction tx = session.beginTransaction();
  283. Book book = (Book) session.load(Book.class,id);
  284. tx.commit();
  285. if (book != null) {
  286. logger.info("Книга: " + book + " найдена.");
  287. } else {
  288. logger.info("Книга c id: " + id + " не существует.");
  289. }
  290.  
  291. return book;
  292. }
  293.  
  294. @SuppressWarnings("unchecked")
  295. public List<Book> getListBooks() {
  296. Transaction tx = session.beginTransaction();
  297. List<Book> bookList = session.createQuery("from Book").list();
  298. tx.commit();
  299. logger.info("Получен список книг:");
  300. for(Book book : bookList) {
  301. logger.info(book.toString());
  302. }
  303.  
  304. return bookList;
  305. }
  306. }
  307.  
  308. package well.dao;
  309.  
  310. import org.hibernate.HibernateException;
  311.  
  312. import org.hibernate.Session;
  313. import org.hibernate.SessionFactory;
  314. import org.hibernate.cfg.Configuration;
  315. import org.springframework.beans.factory.annotation.Autowired;
  316. import org.springframework.stereotype.Repository;
  317.  
  318. @Repository
  319. public class BookSessionFactory {
  320.  
  321. @Autowired
  322. private static final SessionFactory sessionFactory;
  323.  
  324. static {
  325. try {
  326. Configuration configuration = new Configuration();
  327. configuration.configure();
  328.  
  329. sessionFactory = configuration.buildSessionFactory();
  330. } catch (Throwable ex) {
  331. throw new ExceptionInInitializerError(ex);
  332. }
  333. }
  334.  
  335. public static Session getSession() throws HibernateException {
  336. return sessionFactory.openSession();
  337. }
  338. }
  339.  
  340. package well.controllers;
  341.  
  342. import org.springframework.beans.factory.annotation.Autowired;
  343. import org.springframework.beans.factory.annotation.Qualifier;
  344. import org.springframework.stereotype.Controller;
  345. import org.springframework.ui.Model;
  346. import org.springframework.web.bind.annotation.ModelAttribute;
  347. import org.springframework.web.bind.annotation.PathVariable;
  348. import org.springframework.web.bind.annotation.RequestMapping;
  349. import org.springframework.web.bind.annotation.RequestMethod;
  350. import well.model.Book;
  351. import well.service.BookService;
  352.  
  353.  
  354. @Controller
  355. public class MainController {
  356. private BookService bookService;
  357.  
  358. @Autowired
  359. @Qualifier(value = "bookService")
  360. public void setBookService(BookService bookService) {
  361. this.bookService = bookService;
  362. }
  363.  
  364. @RequestMapping(value = "books" , method = RequestMethod.GET)
  365. public String bookList(Model model) {
  366. model.addAttribute("bookList", bookService.getListBooks());
  367. model.addAttribute("newBook",new Book());
  368. return "books";
  369. }
  370.  
  371. @RequestMapping(value = "bookInfo/{id}" , method = RequestMethod.GET)
  372. public String getBookById(@PathVariable("id") int id, Model model) {
  373. Book book = bookService.getBookById(id);
  374. model.addAttribute("book", book);
  375. if (!book.getReadAlready()) book.setReadAlready(true);
  376. bookService.updateBook(book);
  377. return "bookInfo";
  378. }
  379.  
  380. @RequestMapping("/remove/{id}")
  381. public String removeBook(@PathVariable("id") int id){
  382. bookService.removeBook(id);
  383. return "redirect:/books";
  384. }
  385.  
  386. @RequestMapping("edit/{id}")
  387. public String editBook(@PathVariable("id") int id, Model model){
  388. model.addAttribute("newBook", bookService.getBookById(id));
  389. model.addAttribute("bookList", bookService.getListBooks());
  390.  
  391. return "books";
  392. }
  393.  
  394. @RequestMapping(value = "/books/add", method = RequestMethod.POST)
  395. public String addBook(@ModelAttribute("newBook") Book book){
  396. if (book.getAuthor() == "") return "redirect:/books";
  397. book.setReadAlready(false);
  398. if(book.getId() == 0){
  399. bookService.addBook(book);
  400. }else {
  401. bookService.updateBook(book);
  402. }
  403.  
  404. return "redirect:/books";
  405. }
  406. }
  407.  
  408. org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/transaction/spi/TransactionContext] with root cause
  409. java.lang.ClassNotFoundException: org.hibernate.engine.transaction.spi.TransactionContext
Add Comment
Please, Sign In to add comment