Wicket meets Hibernate

I got Hibernate and Wicket working together. It is simple and perhaps still needs some tweaking, but I was happy to have joined the two together. These instructions are intended for Ubuntu, but can be adapted for Windows too. I am especially intrigued that the OpenOffice Database runs on both. You need to have mvn installed too. If not, do the sudo apt-get install maven2 .

I worked for a while with Hibernate, especially with the code from the book titled Harnessing Hibernate. Many of the examples were great and it goes through many details, yet at the end, the Stripes examples don’t seem to work. I ended up at Barnes and Nobel buying a copy of Wicket in Action. I hacked through the Cheesr application, nice concept, yet they seem to leave out how to get the initial infrastructure. What I have really wanted to do is marry the two together.  I actually believe this is a job for Spring, but these things have to occur in steps, so took material from Hibernate Tutorial Refined blog and I was thinking that it shouldn’t be too hard to add the Hibernate to the simple Cheesr application created by the authors of Wicket in Action, Dashorst et al. I grabbed the HibernateUtil class that loads the configuration and session factory. Now, I just needed to load the Cheeses using the Hibernate Query Language. It was simple “from Cheese”, meaning give me everything from the Cheese class. At first, I put the table name “CHEESE”, a mistake! We are dealing with objects, not tables, doh! And, to top it off, I added the database to OpenOffice base. You can start open Office base and open the cheese.odb file and modify the cheeses. You have to restart the Cheesr application for it to take effect, but I find it really cool. The code is very concise too. Yet, I am sure I am missing some exception handling, but I am happy to get this working. So, here are the steps.

  1. Get the code
    $ wget http://brie.com/brian/wicket/zebra03.zip
  2. Unzip it
  3. These are the following Maven commands to start the database, import for Eclipse, compile, clean, and run the application
    $ mvn exec:java -Dexec.mainClass="org.hsqldb.Server" \
      -Dexec.args="-database.0 file:db/data/tutorial -dbname.0 cheeses"
    $ mvn -DdownloadJavadocs=true -DdownloadSources=true eclipse:eclipse
    $ mvn compile
    $ mvn clean
    $ mvn jetty:run

Once you have it running, add the hsqldb.jar file to OpenOffice. In OpenOffice, choose the following menu item.

Tools->Options

You will see the OpenOffice.org->Java item. Select it. To the right, there will be a button labeled Class Path… . Click it. Click the button labeled Add Archive… Locate the hsqldb.jar file in your repository. Maven stores the files it downloads in your $HOME/.m2/repository directory. Navigate to the directory similar to the following and select the hsqldb jar file.

/home/brian/.m2/repository/hsqldb/hsqldb/1.8.0.10/hsqldb-1.8.0.10.jar

Once you have completed this, open the cheese database in OpenOffice. It is named cheeses.odb and you can double click it to open it. Once opened, you can change the price of cheeses in the CHEESE table, restart the Cheesr application and see the changes!

The great thing is how easy Hibernate makes the code look. The tricky part is getting the configuration files. I added the src/main/resources/hibernate.cfg.xml and the src/main/resources/com/brie/dtoo/Cheese.hbm.xml for configuration. In addition, I added the HibernateUtil.java class to do the configuration. In CheesrApplication, I added the private variable along with the initialization code. org.hibernate.Session myHibSession =  HibernateUtil.getSessionFactory().getCurrentSession(); Then, in the CheesrApplication.java constructor, I execute the query that assigns the result to the cheeses list.

In order to access the page, use the following URL. http://localhost:8080/zebra03

Enjoy!

brian

About Brian Lavender

I have a Masters in Computer Science. I like GNU/Linux and different programming languages. I would say at present, I like Java the most.
This entry was posted in Hibernate, Wicket. Bookmark the permalink.

2 Responses to Wicket meets Hibernate

  1. Bob R says:

    Good job, Brian! You’re the man!

Comments are closed.