UP | HOME

BookCasePersistence.java

package de.botzenhart.bookcase.persistence;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import de.botzenhart.bookcase.data.Author;
import de.botzenhart.bookcase.data.Book;
import de.botzenhart.bookcase.data.Keyword;

public class BookCasePersistence {

   public Session getSession() {
      return BookCaseHibernateSessionFactory.getSession();
   }

   public Book saveBook(Book book) {
      Transaction tx = null;
      try {
         // Create some data and persist it
         tx = getSession().beginTransaction();
         Criteria criteria = getSession().createCriteria(Book.class);
         criteria.add(Restrictions.eq("title", book.getTitle()));
         List bl = criteria.list();
         if (bl == null || bl.size() == 0) {
            getSession().save(book);
            tx.commit();
         } else
            book = (Book) bl.get(0);
      } catch (Exception e) {
         if (tx != null) {
            e.printStackTrace();
            tx.rollback();
         }
      }
      return book;
   }

   public Keyword saveKeyword(Keyword keyword) {
      Transaction tx = null;
      try {
         // Create some data and persist it
         tx = getSession().beginTransaction();
         Criteria criteria = getSession().createCriteria(Keyword.class);
         criteria.add(Restrictions.eq("bezeichnung",
               keyword.getBezeichnung()));
         List<Object> bl = criteria.list();
         if (bl == null || bl.size() == 0) {
            getSession().save(keyword);
            tx.commit();
         } else
            keyword = (Keyword) bl.get(0);
      } catch (Exception e) {
         if (tx != null) {
            e.printStackTrace();
            tx.rollback();
         }
      }
      return keyword;
   }

   public Author saveAuthor(Author author) {
      Transaction tx = null;
      try {
         // Create some data and persist it
         tx = getSession().beginTransaction();
         Criteria criteria = getSession().createCriteria(Author.class);
         criteria.add(Restrictions.eq("name", author.getName()));
         List<Object> bl = criteria.list();
         if (bl == null || bl.size() == 0) {
            getSession().save(author);
            tx.commit();
         } else
            author = (Author) bl.get(0);
      } catch (Exception e) {
         if (tx != null) {
            e.printStackTrace();
            tx.rollback();
         }
      }
      return author;
   }

   public List loadBookFromTitle(String title) {
      Transaction tx = null;
      List bookList = null;
      Book book = null;
      book.setTitle(title);
      tx = getSession().beginTransaction();
      Criteria criteria = getSession().createCriteria(Book.class);
      criteria.add(Restrictions.like("TITLE", book.getTitle()));
      bookList = criteria.list();
      return bookList;
   }

}

Author: Rainer Schuler

Date: 2011-02-11 Fr

HTML generated by org-mode 7.4 in emacs 22