Disclaimer

The views and opinions expressed in this blog are my own and do not necessarily reflect those of my employer. The views and opinions expressed by visitors to this blog are theirs and do not necessarily reflect my own

Thursday, January 13, 2011

GWT and JRE managed persistence (JPA) with Apache Derby

A while back I was playing with JPA/EJB3 persistence and GWT (Google Web Toolkit) but didn't want to use a heavy weight Application Server . The idea behind the application I was looking at was that it should be distributable and always run locally.

I downloaded the hibernate and JPA annotations from the hibernate website and thought it would be a simple case of configuring the persistence.xml file. After a some difficulties getting my application to hit the test Derby database I turned to google and it seems others had problems too.

I was expecting to have to create the persistence.xml file under “war/META-INF/” in my eclipse project. This is just wrong, it seems the persistence.xml file has to be on the classpath when your not using container based persistence.

The next problem I encountered was that I'd not included all the necessary jars from the hibernate distribution. The following combination seemed to work for me in the end.

WEB-INF/lib/

  • hibernate3.jar
  • hibernate-jpa-2.0-api.1.0.0.Final.jar
  • javassist-3.12.0.GA.jar
  • jta-1.1.jar
  • slf3j-api-1.6.1.jar

My persistence.xml was as follows:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd" version="1.0">
  <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>  
  <properties>  
   <property name="hibernate.archive.autodetection" value="class" />  
   <property name="hibernate.format_sql" value="true" />  
   <property name="hibernate.show_sql" value="true" />  
   <!-- <property name="hibernate.hbm2ddl.auto" value="create-drop" />-->  
   <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/slicr;" />  
   <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver" />  
   <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />  
   <!-- <property name="hibernate.connection.username" value="user" />  
   <property name="hibernate.connection.password" value="password" /> -->  
  </properties>    
  </persistence-unit>
</persistence>


I was only knocking up a quick and dirty application as a learning exercise but spent a good couple of hours just trying to hit the database. Hopefully this will help others in the same situation.

0 comments: