Posts Tagged ‘hibernate’
Unit test JPA Entities with in-memory Derby/JavaDB
About two years ago I blogged about using HSQLDB to unit test JPA entities. This year, Apache released a Derby version allowing you to use an in memory backend. As I use Derby in software I write, being able to run unit tests on the very same SGBD but in memory is a real gift.
For reference, here is a link to my previous post titled Unit test JPA Entities with in-memory database. What follows is just the very same method applied to Derby.
Glassfish, Hibernate and WebServices – WSGEN Classpath
Following my quick and dirty how-to about using hibernate in glassfish I came across a small pitfall when dealing with metro webservices.
The glassfish’s wsgen classpath on Glassfish is not by domain, ie. the wsgen tool is not run in the domain class-loader.
When trying to deploy a webservice that returns an entity with a hibernate annotation, I got this following error :

Server logs showed the following trace :
/home/paul/Programs/glassfish-v2-b58g/domains/domain1/applications/j2ee-apps/ear-0.1-SNAPSHOT/lib/entities-0.1-SNAPSHOT.jar(app/model/entities/User.class):
warning: Cannot find annotation method 'value()' in type 'org.hibernate.annotations.Fetch':
class file for org.hibernate.annotations.Fetch not found
Problem encountered during annotation processing;
see stacktrace below for more information.
java.lang.NullPointerException
at com.sun.tools.ws.processor.modeler.annotation.WebServiceAP.isSubtype(WebServiceAP.java:360)
...... SNIP ......
at com.sun.enterprise.management.deploy.DeployThread.run(DeployThread.java:223)
error: compilation failed, errors should have been reported
Exception occured in J2EEC Phase
com.sun.enterprise.deployment.backend.IASDeploymentException: WSGEN FAILED
at com.sun.enterprise.webservice.WsUtil.genWSInfo(WsUtil.java:2
Adding the hibernate-annotations.jar file in the glassfish main lib/ directory added the necessary classes to the wsgen classpath.
Unit test JPA Entities with in-memory database
Still about Hibernate, I have several JPA Entities grouped in a Persistence Unit that I need to test. So here is another quick and dirty howto titled “Unit test JPA Entities with in-memory database”.
Here I assume you already have written your entities java classes with correct annotations.
As I’m working with embedded Derby for a year, I first tried with it but procton (thank you procton) pointed me to another lightweight RDBMS.
So, in this how-to we will set up a unit-tests dedicated persistence unit and use HSQLDB as in-memory database for unit tests.
UPDATE: now that Apache Derby provide an in memory backend, I implemented the very same thing with it in a new post : Unit test JPA Entities with in-memory Derby/JavaDB
Quick and dirty howto : Use Hibernate in Glassfish V2
UPDATE:
As Leon pointed in comments, it’s not a good idea to put the hibernate jars in the app server classpath like in this post. Hibernate community had published a howto explaining how to use Hibernate in Glassfish but the link has expired. Here is a copy from google cache:
To use hibernate with glassfish, all you have to do is put hibernate and its dependencies into your EAR file. This might also with with a WAR file.
In your persistence.xml, specify hibernate using this tag:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
You can also specify hibernate properties as usual, for example:
<properties> <property name=”hibernate.hbm2ddl.auto” value=”none”/></properties></blockquote>

For my first post here I’ll write a quick and dirty howto for using Hibernate inside the Glassfish V2 container.
- Download Hibernate projects
- Put’em in the domain’s lib dir
- Change your persistence.xml file
- Enjoy :)
Details are following :
Read the rest of this entry »
