Last couple days I spend some time to set up my ejb3.0 eclipse project. The most hard thing to realize was solving problem with ant classloader and conflicts when defining hibernatetools ant task.
Some common error messages found in debug mode are:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
Type javax.persistence.Entity not present
build.xml:38: Problems in creating a AnnotationConfiguration. Have you remembered to add it to the classpath ?
org.apache.commons.logging.LogConfigurationException: Class org.apache.commons.logging.impl.Jdk14Logger does not implement Log
These are all result of conflicting of missing classpath entries. Also version conflicts in hibernate jars are possible. There was a bug in hibernate ant task impl's in using ClassLoader.
How to get this work:
If you have Eclipse 3.* installed and you also install hibernate tools (or JBoss IDE) you will try to use existing libraries to run ant scripts but … To get out of problems you have to get at least HibernateTools-3.1.0.beta1.zip.
1. Unzip HibernateTools-3.1.0.beta1.zip (don’t unzip it in eclipse directory, this is a step for later). In this example I used E:\Development\tools\hibernate-tools3.1.0beta1\
2. Define ant hibernatetools task in your ant script
Code:
<property name="hib-tools.plugin" value="E:\Development\tools\hibernate-tools3.1.0beta1\plugins\org.hibernate.eclipse_3.1.0.beta1"></property>
<path id="hibernatetools.path">
<fileset dir="${hib-tools.plugin}\lib\tools" includes="hibernate-tools.jar" />
<fileset dir="${hib-tools.plugin}\lib\hibernate" includes="hibernate3.jar" />
<fileset dir="${hib-tools.plugin}\lib\hibernate" includes="commons-logging-1.0.4.jar" />
<fileset dir="${hib-tools.plugin}\lib\annotations" includes="hibernate-annotations.jar" />
<fileset dir="${hib-tools.plugin}\lib\hibernate" includes="dom4j-1.6.1.jar" />
<fileset dir="${hib-tools.plugin}\lib\annotations" includes="ejb3-persistence.jar" />
<fileset dir="${hib-tools.plugin}\lib\hibernate" includes="commons-collections-2.1.1.jar" />
<fileset dir="E:\development\eclipse\plugins\org.jboss.ide.eclipse.jdt.j2ee.core_1.5.0.M2\lib\j2ee-1.4" includes="jta-api.jar" />
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="hibernatetools.path" >
</taskdef>
Where ${lib.dir} is folder where jdbc driver jar is stated.
3. Make hibernate.cfg.hbm (use hibernate plugin New -> Hiberante Configuration File)
4. Annotate you Entity classes with hibernates Entity annotation. Note is you are using EJB3.0 you already have javax.persistence.Entity annotation. You have to add hibernates Entity annotation to.
5. Define your target
Code:
<target name="hib" >
<hibernatetool destdir="hibernate" >
<classpath>
<path location="bin"></path>
</classpath>
<annotationconfiguration configurationfile="hibernate\hibernate.cfg.xml" />
<hbm2ddl drop="false" outputfilename="dvd.sql" />
</hibernatetool>
</target>
That’s it.
Not yet tested: it would be nice to update hibernatetools distribution in you eclipse installation.