Hello everyone. I've read the how to ask for help guide, but please go easy on me -- this is my first post for help with Hibernate.
I'm trying to set up a project using Hibernate Annotations, and I'm basing it off the Hibernate tutorial helloworld-jpa and caveatempor-jpa. Like the tutorials, we are using HyperSonic SQL (HSQL, which comes with JBoss' app server) as the backend database for development. Also like the tutorial, I'm trying to come up with a task that will automatically destroy the database (drop everything) and re-create it using the annotated class files. I thought I had it all set up, but I'm running into a problem where the hibernate tool hbm2dll is claiming it cannot find my persistence unit.
Hibernate version:
I'm using the latest stable version of Hibernate Core, Annotations, and Entity Manager.
Mapping documents:
Here's what my persistence.xml file looks like. This is located in the META-INF folder included (i believe) in my class path.
Code:
<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
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="cpapersistenceunit">
<!-- The provider only needs to be set if you use several JPA providers
<provider>org.hibernate.ejb.HibernatePersistence</provider>
-->
<!-- This is required to be spec compliant, Hibernate however supports
auto-detection even in JSE.
<class>mil.ustranscom.cpa.Office</class>
-->
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<!-- SQL stdout logging
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
-->
<property name="hibernate.connection.driver_class"
value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.url"
value="jdbc:hsqldb:hsql://localhost"/>
<property name="hibernate.connection.username"
value="sa"/>
<property name="hibernate.c3p0.min_size"
value="5"/>
<property name="hibernate.c3p0.max_size"
value="20"/>
<property name="hibernate.c3p0.timeout"
value="300"/>
<property name="hibernate.c3p0.max_statements"
value="50"/>
<property name="hibernate.c3p0.idle_test_period"
value="3000"/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.HSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
Here is what my ant build.xml file looks like. I've removed some targets so there's less to actually go through.
Code:
<project name="cpa" default="war">
<property environment="env"/>
<!-- Set environment variables to match the parts in CAPS (e.g., JBOSS_HOME) to override defaults -->
<!-- (For the curious, this relies on the fact that properties in Ant are immutable; if the
environment variable is set, subsequent assignments to that property will have no effect -->
<property name="env.JBOSS_HOME" location="C:\jboss-4.2.2.GA" />
<property name="env.TOMCAT_HOME" location="/hsql"/>
<property name="cpa.dir" value="${eclipse.workspace}/cpa"/>
<!-- Deployment related values -->
<property name="jboss.home" location="${env.JBOSS_HOME}"/>
<property name="jboss.server.dir" value="${jboss.home}/server/default"/>
<property name="jboss.deploy.dir" value="${jboss.server.dir}/deploy"/>
<property name="jboss.conf.dir" value="${jboss.server.dir}/conf"/>
<property name="jboss.cache.dir1" value="${jboss.server.dir}/work/jboss.web"/>
<property name="jboss.cache.dir2" value="${jboss.server.dir}/tmp/deploy"/>
<property name="prefix" value="usn"/>
<!-- Build related values -->
<property name="project.name" value="cpa"/>
<property name="source.dir" value="src"/>
<property name="classes.dir" value="classes"/>
<property name="conf.dir" value="conf"/>
<property name="dist.dir" value="dist"/>
<property name="cpalibs.dir" value="lib"/>
<property name="meta.dir" value="${source.dir}/META-INF" />
<property name="eclipse.workspace.dir" value="${eclipse.workspace}"/>
<property name="app.ear" value="cpa.ear"/>
<property name="app.war" value="cpa.war"/>
<property name="src.gen.dir" value="generated-src" />
<property name="class.gen.dir" value="generated-build"/>
<property name="hbm.dir" value="${source.dir}/WEB-INF/classes"/>
<!-- Useful shortcuts -->
<patternset id="meta.files">
<include name="**/*.xml"/>
<include name="**/*.properties"/>
</patternset>
<!--CLASSPATHS-->
<!-- compile classpath -->
<path id="compile.class.path">
<fileset dir="${cpalibs.dir}">
<include name="**/*.jar" />
</fileset>
<!-- <fileset dir="${meta.dir}">
<include name="**/persistence.xml" />
</fileset>
<pathelement location="${meta.dir}" /> -->
<pathelement location="${class.gen.dir}"/>
<pathelement location="${classes.dir}"/>
<pathelement location="${conf.dir}"/>
</path>
<!--CLEAN TARGETS-->
<target name="clean">
<delete includeEmptyDirs="true" quiet="true">
<fileset dir="dist"/>
<fileset dir="${classes.dir}"/>
<fileset dir="${src.gen.dir}"/>
<fileset dir="${class.gen.dir}"/>
</delete>
</target>
<!--This will clean any files cached in JBoss.-->
<target name="clean.jboss.cache">
<delete dir="${jboss.cache.dir2}" quiet="true"/>
<delete dir="${jboss.cache.dir1}" quiet="true"/>
</target>
<!-- Prepare needed directories -->
<target name="prepare" depends="clean">
<mkdir dir="${classes.dir}"/>
<mkdir dir="${class.gen.dir}"/>
<mkdir dir="${src.gen.dir}"/>
<mkdir dir="dist"/>
</target>
<!--COPY TARGETS-->
<target name="copy.src.files" depends="prepare">
<copy todir="${src.gen.dir}">
<fileset dir="${source.dir}">
<include name="**/*.java"/>
</fileset>
</copy>
</target>
<!-- Copy metadata to build classpath -->
<target name="copymetafiles" depends="copy.src.files">
<copy todir="${src.gen.dir}">
<fileset dir="${source.dir}">
<patternset refid="meta.files"/>
</fileset>
</copy>
<!--<copy todir="${build.dir}">
<fileset dir="${src.etc.dir}">
<patternset refid="meta.files"/>
</fileset>
</copy> -->
</target>
<!--COMPILE TARGETS-->
<!--Compile everything in src.gen.dir-->
<target name="compile" depends="copy.src.files">
<javac srcdir="${src.gen.dir}" destdir="${class.gen.dir}" source="1.5" target="1.5">
<classpath refid="compile.class.path"/>
<include name="**/*.java/"/>
</javac>
</target>
<!-- Hibernate Tools import -->
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="compile.class.path"/>
<!-- Export the database schema -->
<target name="schemaexport" depends="compile, copymetafiles"
description="Exports a generated schema to DB and file">
<hibernatetool destdir="${class.gen.dir}">
<classpath path="compile.class.path"/>
<jpaconfiguration persistenceunit="cpapersistenceunit"/> <!-- Use META-INF/persistence.xml -->
<hbm2ddl
drop="true"
create="true"
export="true"
outputfilename="${project.name}-ddl.sql"
delimiter=";"
format="true"/>
</hibernatetool>
</target>
<!--PACKAGING TARGETS-->
<target name="war" depends="compile">
<war destfile="dist/${app.war}" webxml="${source.dir}/WEB-INF/web.xml">
<fileset dir="${source.dir}">
<include name="**/*.jsp"/>
<include name="*.html"/>
<include name="**/*.properties"/>
<include name="**/*.gif"/>
<include name="**/*.jpg"/>
<include name="**/*.pdf"/>
<include name="**/*.hbm"/>
<include name="**/*.hmz"/>
<include name="**/*.css"/>
</fileset>
<webinf dir="${source.dir}/WEB-INF/">
<include name="cpaDispatcher-servlet.xml"/>
<include name="applicationContext-cpa.xml"/>
</webinf>
<!--<metainf dir="${source.dir}/META-INF/">
<include name="context.xml"/>
<exclude name="application.xml"/>
</metainf>-->
<classes dir="${class.gen.dir}">
<include name="**/*.class"/>
</classes>
<lib dir="${cpalibs.dir}">
<include name="spring*.jar"/>
<include name="j2ee.jar"/>
<include name="servlet-api.jar"/>
</lib>
</war>
</target>
<target name="ear" depends="war">
<ear earfile="dist/${app.ear}" appxml="${source.dir}/META-INF/application.xml">
<fileset dir="dist" includes="${app.war}"/>
</ear>
</target>
</project>
Code between sessionFactory.openSession() and session.close():
It really doesn't get this far. The schemaexport task fails -- it builds and compiles okay, its just that the Hibernate tool complains.
Full stack trace of any exception that occurs:
Here is what happens when I run the task from the commandline using Ant 1.7.0 and the -verbose option enabled:
Quote:
schemaexport:
[hibernatetool] Executing Hibernate Tool with a JPA Configuration
dropping C:\Documents and Settings\wilsonc\workspace\cpa\compile.class.path from
path as it doesn't exist
[hibernatetool] 1. task: hbm2ddl (Generates database schema)
[hibernatetool] An exception occurred while running exporter #2:hbm2ddl (Generat
es database schema)
[hibernatetool] To get the full stack trace run ant with -verbose
[hibernatetool] Persistence unit not found: 'cpapersistenceunit'.
BUILD FAILED
C:\Documents and Settings\wilsonc\workspace\cpa\build.xml:141: Persistence unit
not found: 'cpapersistenceunit'.
at org.hibernate.tool.ant.JPAConfigurationTask.createConfiguration(JPACo
nfigurationTask.java:44)
at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(Configurati
onTask.java:54)
at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateTo
olTask.java:302)
at org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hbm2DDLExporterTas
k.java:45)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.ja
va:186)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.jav
a:105)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:357)
at org.apache.tools.ant.Target.performTasks(Target.java:385)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1329)
at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExe
cutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
at org.apache.tools.ant.Main.runBuild(Main.java:698)
at org.apache.tools.ant.Main.startAnt(Main.java:199)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
Total time: 3 seconds
Name and version of the database you are using:Its the version of HSQL that ships with the latest verion of JBoss, version 4.2.2.G.A.
The generated SQL (show_sql=true):Debug level Hibernate log excerpt:It doesn't get that far. If I'm wrong, please correct me. I'm more than willing to do my share to help solve this problem so my team can be up and running using Hibernate. I realize this is mostly a property and configuration problem, so I thought I'd ask someone with more experience if they see anything wrong with my ant/hibernate configuration.
Quote: