Some corrections to Hibernate tutorial

Reading Time: 2 minutes

Hibernate logo
I was referring to the First application at Hibernate (Jboss) offical documentation. I found some problems when doing the exercise: just want to share some of my findings so others may benefit.

1) dependency version missing: there are already some some discussions on the Hibernate discussion forum, such as this one and that one. I followed the first one: added the version numbers as suggested and the problem went away.

2) jdbc driver can not be found, the fix is to

mvn install:install-file -DgroupId=hsqldb -DartifactId=hsqldb -Dversion=1.8.1.2 -Dpackaging=jar -Dfile=/path/to/your/hsqldb.jar

in my case (mac) this is /Users/major_xu/Downloads/hsqldb/lib/hsqldb.jar

I saw discussion on this one, too, the suggestion is to “add classpath for hsqldb.jar”. But I don’t know how to do it in maven command line.

3) misc: there are a lot other corrections:

mapping in Hibernate configuration, the tutorial says
< mapping resource=”events/Event.hbm.xml”/ >
< mapping resource=”events/Person.hbm.xml”/ >

It should follow the format of earlier “hibernate.cfg.xml” instead, and no need to include Event.hbm.xml twice.
< mapping resource=”org/hibernate/tutorial/domain/Event.hbm.xml”/ >

By the same token, “Person.hbm.xml” should be placed in the same place as “Event.hbm.xml”

Person.java: need to add getter and setter method for “id” and “age”. Otherwise, I am getting an error like this:
Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

EventManager.java: addPersonToEvent goes like this
private void addPersonToEvent(Long personId, Long eventId) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

Person aPerson = (Person) session.load(Person.class, personId);
Event anEvent = (Event) session.load(Event.class, eventId);
aPerson.getEvents().add(anEvent);

session.getTransaction().commit();
}

4. To be done:
I still need to make log4j work, and I’d like to have Eclipse working with Maven (right now Eclipse seems not like the extra java/main directory which is required by Maven).

Email me at minjie DOT xu AT gmail DOT com if you would like a copy of the code.

Appendix:

Setup Maven on Mac
http://maven.apache.org/download.html#Installation

(use the version you have at your mac, in my case 1.5.0)
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home

http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

HSQLDB server setup and DB manager

Click to access Hibernate-Spring-Maven-Eclipse-Tutorial.pdf


do this in the hsqldb installation dir
java -cp ./lib/hsqldb.jar org.hsqldb.Server

and to see the database in action, run database manager UI (from the lib dir which has hsqldb.jar)
java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing

I used the above commands in stead of the Maven commands mentioned in the documentation.
mvn exec:java -Dexec.mainClass=”org.hsqldb.Server” -Dexec.args=”-database.0 file:target/data/tutorial”

(Update 07-14-2021, note this is 11+ years later): I am back to old Hibernate code, the tutorial above was done 2 laptops ago, and the screen of my 1st MacBook (white) was broke. Managed to zipped up the file and used USB drive to  transfer to late 2012 model MacBook Pro, and further uploaded it to Google drive. Here are what I accomplished so far:

at

/Users/minjie/.m2/repository/org/hsqldb/hsqldb/1.8.0.10

java -classpath hsqldb-1.8.0.10.jar org.hsqldb.Server (starts up DB server)

and

java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing (starts up the DB Java Swing UI)

Leave a comment

Your email address will not be published. Required fields are marked *