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.
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.