Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="UTF-8"?>
- <project version="4">
- <component name="DataSourceManagerImpl" format="xml" multifile-model="true">
- <data-source source="LOCAL" name="welldb" uuid="78793bdd-e26d-42e7-95d9-c2a44584c136">
- <driver-ref>mysql</driver-ref>
- <synchronize>true</synchronize>
- <jdbc-driver>com.mysql.jdbc.Driver</jdbc-driver>
- <jdbc-url>jdbc:mysql://localhost:3306/test</jdbc-url>
- <driver-properties>
- <property name="autoReconnect" value="true" />
- <property name="zeroDateTimeBehavior" value="convertToNull" />
- <property name="tinyInt1isBit" value="false" />
- <property name="characterEncoding" value="utf8" />
- <property name="characterSetResults" value="utf8" />
- <property name="yearIsDateType" value="false" />
- </driver-properties>
- </data-source>
- </component>
- </project>
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
- <context:component-scan base-package = "well.controllers" />
- <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name = "prefix" value = "/WEB-INF/jsp/" />
- <property name = "suffix" value = ".jsp" />
- </bean>
- <bean id="sessionFactory" class="well.dao.BookSessionFactory"/>
- <bean id="bookDao" class="well.dao.BookDaoImpl">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <bean id="bookService" class="well.service.BookServiceImpl">
- <property name="bookDao" ref="bookDao"/>
- </bean>
- <mvc:default-servlet-handler/>
- <mvc:annotation-driven/>
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
- p:sessionFactory-ref="sessionFactory"/>
- </beans>
- package well.model;
- import javax.persistence.*;
- import java.util.Objects;
- @Entity
- @Table(name = "library")
- public class Book {
- private Integer id;
- private String title;
- private String description;
- private String author;
- private String isbn;
- private Integer printYear;
- private Boolean readAlready;
- @Id
- @Column(name = "id")
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- @Basic
- @Column(name = "title")
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- @Basic
- @Column(name = "description")
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- @Basic
- @Column(name = "author")
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- @Basic
- @Column(name = "isbn")
- public String getIsbn() {
- return isbn;
- }
- public void setIsbn(String isbn) {
- this.isbn = isbn;
- }
- @Basic
- @Column(name = "printYear")
- public Integer getPrintYear() {
- return printYear;
- }
- public void setPrintYear(Integer printYear) {
- this.printYear = printYear;
- }
- @Basic
- @Column(name = "readAlready")
- public Boolean getReadAlready() {
- return readAlready;
- }
- public void setReadAlready(Boolean readAlready) {
- this.readAlready = readAlready;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Book book = (Book) o;
- return id == book.id &&
- Objects.equals(title, book.title) &&
- Objects.equals(description, book.description) &&
- Objects.equals(author, book.author) &&
- Objects.equals(isbn, book.isbn) &&
- Objects.equals(printYear, book.printYear) &&
- Objects.equals(readAlready, book.readAlready);
- }
- @Override
- public int hashCode() {
- return Objects.hash(id, title, description, author, isbn, printYear, readAlready);
- }
- @Override
- public String toString() {
- return "Book{" +
- "id=" + id +
- ", title='" + title + ''' +
- ", description='" + description + ''' +
- ", author='" + author + ''' +
- ", isbn='" + isbn + ''' +
- ", printYear=" + printYear +
- ", readAlready=" + readAlready +
- '}';
- }
- }
- package well.service;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import well.dao.BookDao;
- import well.model.Book;
- import java.util.List;
- @Service
- public class BookServiceImpl implements BookService {
- private BookDao bookDao;
- public void setBookDao(BookDao bookDao) {
- this.bookDao = bookDao;
- }
- @Transactional
- public void addBook(Book book) {
- bookDao.addBook(book);
- }
- @Transactional
- public void updateBook(Book book) {
- bookDao.updateBook(book);
- }
- @Transactional
- public void removeBook(int id) {
- System.out.println("remove " + id);
- bookDao.removeBook(id);
- }
- @Transactional
- public Book getBookById(int id) {
- return bookDao.getBookById(id);
- }
- @Transactional
- public List<Book> getListBooks() {
- return bookDao.getListBooks();
- }
- }
- package well.dao;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import well.model.Book;
- import java.util.List;
- @Service
- public class BookDaoImpl implements BookDao {
- private SessionFactory sessionFactory;
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- private static final Logger logger = LoggerFactory.getLogger(BookDaoImpl.class);
- // private static Session session = BookSessionFactory.getSession();
- private Session session = sessionFactory.getCurrentSession();
- public void addBook(Book book) {
- Transaction tx = session.beginTransaction();
- session.persist(book);
- tx.commit();
- logger.info("Книга: " + book + " успешно добавлена.");
- }
- public void updateBook(Book book) {
- Transaction tx = session.beginTransaction();
- session.merge(book);
- tx.commit();
- logger.info("Книга: " + book + " успешно обновлена.");
- }
- public void removeBook(int id) {
- Book book = getBookById(id);
- logger.info("Книга: " + book);
- if (book != null) {
- Transaction tx = session.beginTransaction();
- session.delete(book);
- tx.commit();
- logger.info("Книга: " + book + " успешно удалена.");
- }
- }
- public Book getBookById(int id) {
- Transaction tx = session.beginTransaction();
- Book book = (Book) session.load(Book.class,id);
- tx.commit();
- if (book != null) {
- logger.info("Книга: " + book + " найдена.");
- } else {
- logger.info("Книга c id: " + id + " не существует.");
- }
- return book;
- }
- @SuppressWarnings("unchecked")
- public List<Book> getListBooks() {
- Transaction tx = session.beginTransaction();
- List<Book> bookList = session.createQuery("from Book").list();
- tx.commit();
- logger.info("Получен список книг:");
- for(Book book : bookList) {
- logger.info(book.toString());
- }
- return bookList;
- }
- }
- package well.dao;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Repository;
- @Repository
- public class BookSessionFactory {
- @Autowired
- private static final SessionFactory sessionFactory;
- static {
- try {
- Configuration configuration = new Configuration();
- configuration.configure();
- sessionFactory = configuration.buildSessionFactory();
- } catch (Throwable ex) {
- throw new ExceptionInInitializerError(ex);
- }
- }
- public static Session getSession() throws HibernateException {
- return sessionFactory.openSession();
- }
- }
- package well.controllers;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import well.model.Book;
- import well.service.BookService;
- @Controller
- public class MainController {
- private BookService bookService;
- @Autowired
- @Qualifier(value = "bookService")
- public void setBookService(BookService bookService) {
- this.bookService = bookService;
- }
- @RequestMapping(value = "books" , method = RequestMethod.GET)
- public String bookList(Model model) {
- model.addAttribute("bookList", bookService.getListBooks());
- model.addAttribute("newBook",new Book());
- return "books";
- }
- @RequestMapping(value = "bookInfo/{id}" , method = RequestMethod.GET)
- public String getBookById(@PathVariable("id") int id, Model model) {
- Book book = bookService.getBookById(id);
- model.addAttribute("book", book);
- if (!book.getReadAlready()) book.setReadAlready(true);
- bookService.updateBook(book);
- return "bookInfo";
- }
- @RequestMapping("/remove/{id}")
- public String removeBook(@PathVariable("id") int id){
- bookService.removeBook(id);
- return "redirect:/books";
- }
- @RequestMapping("edit/{id}")
- public String editBook(@PathVariable("id") int id, Model model){
- model.addAttribute("newBook", bookService.getBookById(id));
- model.addAttribute("bookList", bookService.getListBooks());
- return "books";
- }
- @RequestMapping(value = "/books/add", method = RequestMethod.POST)
- public String addBook(@ModelAttribute("newBook") Book book){
- if (book.getAuthor() == "") return "redirect:/books";
- book.setReadAlready(false);
- if(book.getId() == 0){
- bookService.addBook(book);
- }else {
- bookService.updateBook(book);
- }
- return "redirect:/books";
- }
- }
- 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
- java.lang.ClassNotFoundException: org.hibernate.engine.transaction.spi.TransactionContext
Add Comment
Please, Sign In to add comment