I have defined two sets of hbm files. In my build script, I first generate pojos from one set of hbm files using hbm2java. This works perfectly. Then I try to generate pojos from the other set of hbm files. The second set of hbm files include some entities that are associated with entities defined in the first set. For example, there is many-to-one in one hbm file as the following:
<many-to-one name="department" class="Department" fetch="select" property-ref="deptId"> <column name="employeedepartment"/> </many-to-one>
where the class "Department" refers to a pojo class generated from the first set of hbm files.
I did two experiments.
1. When I generate pojos for the second set of hbm files, I include all hbm files from the first set and put them in the source directory. This works but not what I want. The reason I do not want this is because hbm2java generates pojos for both set of hbm files.
2. Contrast to the first experiment, I do not put the first set of hbm files in the source directory. Instead, I build a jar file that contains the first set of hbm files and their corresponding pojo classes. Then I include the jar as a dependency for the maven plugin that is to generate pojos from the second set of hbm files. What I hope is that hbm2java will generate pojos for the second set of hbm files. But it does not work. When I run the build, there is NPE as the following:
Caused by: java.lang.NullPointerException at org.hibernate.mapping.ManyToOne.createPropertyRefConstraints(ManyToOne.java:71) at org.hibernate.cfg.HbmBinder$ManyToOneSecondPass.doSecondPass(HbmBinder.java:2837) at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1716) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423) at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1375) 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.Hbm2JavaExporterTask.configureExporter(Hbm2JavaExporterTask.java:34) at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:39) at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
It looks like hbm2java can not resolve the referred class in many-to-one generated from the first set of hbm files although those classes are placed in the classpath. Correct me if I am wrong.
So my question is if hbm2java supports the case in the second experiment. If yes, what is the right way?
I also include the build script here in case it helps. Note: all dependencies are omitted for saving spaces.
I appreciate your help in advance.
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>hibernate3-maven-plugin</artifactId> <version>3.0</version> <executions> <execution> <id>Generate hbm.cfg.xml</id> <phase>generate-sources</phase> <goals> <goal>run</goal> </goals> <configuration> <hibernatetool templatepath="${rootConfigDir}/templates"> <jdbcconfiguration propertyfile="${rootConfigDir}/hibernate.properties" revengfile="${rootConfigDir}/hibernate.reveng.xml" detectmanytomany="true" detectOptimisticLock="true" /> <hbm2java jdk5="true" ejb3="false" destdir="${basedir}/target/generated-sources/src/main/java"> <configuration propertyfile="${rootConfigDir}/hibernate.properties"> <fileset dir="${basedir}/target/generated-sources/src/main/resources"> <include name="**/*.hbm.xml" /> </fileset> </configuration> </hbm2java> </hibernatetool> </configuration> </execution> </executions> </plugin>
|