File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Hibernate, Spring and H2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hibernate, Spring and H2" Watch "Hibernate, Spring and H2" New topic

Hibernate, Spring and H2

Seamus Minogue
Ranch Hand

Joined: Jun 24, 2008
Posts: 41
First off if this is the wrong place... sorry. I didnt think it really fit in the Hibernate area of the forum.

I am writing test cases for DAO's in a project. Rather than simulate the database access I really am going to a database.

My application will eventually be running on a MySql database, but I dont want to use that for testing. So at present when my test cases run spring calls derby which creates a new database. By using Hibernate's property I am creating my table structure and everything works fine happiness, right?

So my problem is (other than derby creating this annoying directory in my project for the database) derby seems very slow. At present my project is executing 81 tests of the DAO's in just over 8.5 seconds. Now I can live with 8.5 seconds delay in the build. The problem is I am only testing about 33% of the DAO. So I expect that number to probably triple. Again, 25 seconds delay during build is something I can live with. I am thinking down the road when I start testing other things like the service layer of my application (which trickles down to the DAO and DB) that number is going to go up. I really dont want to end up having a build which takes several minutes to execute!

So I have been looking at switching from derby (file based) to H2Database which according to their website (they wouldn't stretch the truth would they?) is the fastest in memory DB solution out there.

The problem I am running into is that when I execute my tests I get an error that my schema does not exist and my tests fail.

Has anyone gotten this to work? If not has anyone done something similar and can tell me what in memory DB they used and how?


Seamus Minogue
Ranch Hand

Joined: Jun 24, 2008
Posts: 41
Ok so I worked this out and thought I would share with all those who have suffered as I have suffered.



JUnit Test Class:

The key seemed to be removing the catelog attribute of class tag in the hibernate-mapping file.

I dont really know why this broke things... But removing it fixed the problem. I dont know if its required when the application actually connects to my real database rather than this one in memory. This is part of a utility jar file and the mappings will be external to it when I actually deploy.

Anyway, my 81 tests went from like 8.5 seconds to execute down to 3.106 seconds. BIG improvement :-)

Hope this helps someone.


I agree. Here's the link:
subject: Hibernate, Spring and H2
It's not a secret anymore!