Saturday, July 10, 2010

Using JPA and Hibernate

So yes. Hibernate is one of the most important actors in JPA specification. Emmanuel Bernard from JBoss presented some new things on Hibernate especially its relationship to JPA on SophiaConf last Friday.  As he said in his tweeter, if you were not there, you've wasted your time. I was there, so I didn't waste my time :-)

OK. That's one thing. Now, let's see how things work with JPA and Hibernate. First, you will need at least 3.5.0 version of Hibernate Entity Manager. The POM looks like the following:


You may need to define the repository:


And, here is my boring Flight class:

package com.arizal.flight;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

public class Flight {
  private String number;
  private String airline;

  private Airport departure;

  private Airport arrival;

  @Id private int id;
And Airport:

public class Airport {
  @Id private String code;
  private String name;

  public void setCode(String code) {
    this.code = code;

  public String getCode() {
    return code;

  public void setName(String name) { = name;

  public String getName() {
    return name;

Then, put the HSQL DB in runtime dependency:

This should be enough for us to write the code to persist flight, for example the following:

public static void main(String args[]) {
    try {
      Flight flight = new Flight();

      EntityManagerFactory emf = 
      EntityManager em = emf.createEntityManager();

    catch (Exception ex) {
I ran the code, but got this error:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See for further details.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
at org.slf4j.LoggerFactory.getSingleton(
at org.slf4j.LoggerFactory.bind(
at org.slf4j.LoggerFactory.performInitialization(
at org.slf4j.LoggerFactory.getILoggerFactory(
at org.slf4j.LoggerFactory.getLogger(
at org.slf4j.LoggerFactory.getLogger(

Not very cool.  OK, I miss something on slf4j. Add it to the pom.xml as follow solves the problem:
Yes. Now it works.

O, yes. I forgot to tell something. The persistence.xml accepts the JPA properties:

<persistence-unit name="jpa" >  
            <property name="javax.persistence.jdbc.driver
            <property name="javax.persistence.jdbc.url
            <property name="javax.persistence.jdbc.user" value="SA"/>
            <property name="javax.persistence.jdbc.password" value=""/>
             <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>

It was not the case in older version of hibernate entity manager that requires the following properties instead:
    <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
    <property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost/xdb" />
    <property name="hibernate.connection.username" value="sa" />
    <property name="hibernate.connection.password" value="" />

Pretty cool, isn't it ?

No comments: