I have to implement ant base task to generate hibernate mapping (hbm.xml ) to a java bean when the bean given. So with the hibernate-tools i have successfully done that. But to annotate the pojo i have to use jpa 2.0 annotations (hibernate-jpa-2.0-api-1.0 from this jar)
Now My problem is Do we always have to use jpa annotations when we need to use hbm2hbmxml ? Cant we use hibernate annotations instead (ones in org.hibernate.annotations package)
because when i remove hibernate-jpa-2.0-api-1.0.jar from my classpath and use hibernate annotation to annotate the POJO it gives me following error
Code:
Executing Hibernate Tool with a Hibernate Annotation/EJB3 Configuration
[hibernatetool] 1. task: hbm2hbmxml (Generates a set of hbm.xml files)
[hibernatetool] log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
[hibernatetool] log4j:WARN Please initialize the log4j system properly.
BUILD FAILED
java.lang.NoClassDefFoundError: javax/persistence/Entity
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:253)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1125)
at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:56)
at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:302)
at org.hibernate.tool.ant.HibernateToolTask.getProperties(HibernateToolTask.java:318)
at org.hibernate.tool.ant.ExporterTask.configureExporter(ExporterTask.java:94)
at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:39)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
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:1337)
at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
at org.apache.tools.ant.Main.runBuild(Main.java:758)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
Caused by: java.lang.ClassNotFoundException: javax.persistence.Entity
at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1400)
at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1341)
at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:1088)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 24 more
My ant task is as follows
Code:
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="libraries" />
<target name="init" depends="clean, compile">
<hibernatetool destdir="${targetdir}">
<classpath>
<!-- it is in this classpath you put your classes dir, and/or jpa persistence compliant jar -->
<path location="${basedir}/bin" />
</classpath>
<annotationconfiguration configurationfile="${basedir}/src/hibernate.cfg.xml" />
<hbm2hbmxml />
</hibernatetool>
</target>