UP | HOME

TestHibernateBookcase.java

package de.botzenhart.bookcase.test.persistence;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

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

public class TestHibernateBookcase {

   private static Logger logger = Logger.getLogger("testHibernateBookcase");

   public void testDbConnection() {
      logger.setLevel(Level.INFO);
      String klappenText = "Transactions also group data access operations, in fact, every SQL"
            + "statement, be it queries or DML, has to execute inside a database transaction. "
            + "There can be no communication with a database outside of a database transaction. "
            + "(Note that there are such things as read-only transactions, that can be used to "
            + "improve cleanup time in a database engine if it is not smart enough to optimize its own operations.)";
      Author author = new Author("Hans");
      Set<Author> authors = new HashSet<Author>();
      authors.add(author);
      Book book = new Book("Hibernate", klappenText, null, authors,
            new Date());
      BookCasePersistence bcp = new BookCasePersistence();
      Keyword keyword = bcp.saveKeyword(new Keyword("Hibernate"));
      logger.info(keyword.toString());
      System.out.println(keyword);
      author = bcp.saveAuthor(author);
      logger.info(author.toString());
      System.out.println(author);
      book = bcp.saveBook(book);
      logger.info(book.toString());
      System.out.println(book);      
      // in HSQL you need to send a SHUTDOWN command to save the changes
      try {
                bcp.getSession().connection().createStatement().execute("SHUTDOWN");
        } catch (HibernateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }


   }

   public static void main(String[] args) {

      TestHibernateBookcase thb = new TestHibernateBookcase();
      thb.testDbConnection();
   }
}

Author: Rainer Schuler

Date: 2011-02-11 Fr

HTML generated by org-mode 7.4 in emacs 22