views:

43

answers:

1

Hi.

I am trying to migrate my ant build to maven2. in my build.xml I invoke the hbm2java in the following way:

<hibernatetool destdir="/src/generated/">
        <configuration configurationfile="${env.ITP_HOME}/core/xml/hibernate/hibernate.cfg.xml">
            <fileset dir="/xml/hibernate">
                <include name="*.hbm.xml"/>
            </fileset>
        </configuration>
        <hbm2java/>
    </hibernatetool>

my hibernate.cfg.xml is:

<hibernate-configuration>
<session-factory>
 <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>        
</session-factory>    

in my maven2 POM file I have:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
 <execution>
  <id>hbm2java</id>
  <phase>generate-sources</phase>
  <goals>
   <goal>hbm2java</goal>
  </goals>
  <configuration>
   <components>
    <component>
     <name>hbm2java</name>
     <implementation>configuration</implementation>
     <outputDirectory>/src/main/java</outputDirectory>
    </component>
   </components>
   <componentProperties>
    <jdk5>true</jdk5>
    <configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile>
   </componentProperties>
  </configuration>      
 </execution>

but when executing mvn hibernate3:hbm2java i see no files get generated unless they are all listed in hibernate.cfg.xml. Is there a way to specify a fileset in the maven configuration similar to the ant task?

thanks, naor

+1  A: 

I'm not sure this is the only way but I would use hbm2cfgxml first to generate a hibernate.cfg.xml configuration file including the <mapping resource="..."/> entries and then the hbm2java goal to generate the POJOs. Below, a configuration doing this as part of your build:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>hibernate3-maven-plugin</artifactId>
  <version>2.2</version>
  <executions>
    <execution>
      <id>generate-xml-files</id>
      <phase>generate-resources</phase>
      <goals>
        <goal>hbm2cfgxml</goal>
      </goals>
    </execution>
    <execution>
      <id>generate-entities</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>hbm2java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <components>
      <component>
        <name>hbm2cfgxml</name>
        <implementation>jdbcconfiguration</implementation>
        <outputDirectory>target/classes</outputDirectory>
      </component>
      <component>
        <name>hbm2java</name>
        <implementation>configuration</implementation>
        <outputDirectory>target/generated-sources/hibernate3</outputDirectory>
      </component>
    </components>
    <componentProperties>
      <propertyfile>src/main/resources/database.properties</propertyfile>
      <jdk5>true</jdk5>
      <ejb3>false</ejb3>
      <packagename>com.mycompany.myapp</packagename>
      <format>true</format>
      <haltonerror>true</haltonerror>
    </componentProperties>
  </configuration>
  <dependencies>
    <!-- your JDBC driver -->
    ...
  </dependencies>
</plugin>

Where the src/main/database.properties file contains the following information

hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.url=...
hibernate.connection.username=...
hibernate.connection.password=...
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

This setup assumes your .hbm.xml are placed in src/main/resources (and will thus be copied into target/classes for the processing by hbm2java).

Pascal Thivent
Thanks for the answer Pascal.I forgot to mention that my ant builds also generates my ddl script to build the database as part of the build (different target). I wish to do a similar thing in my maven2 build using hbm2ddl.Does your solution requires me to have my database already built to generate the cfg.xml? if so, is there a way to achieve this without pointing to an existing schema?
naor
@naor I am not sure it does (the database connection info will be used to create the hibernate.cfg.xml), and I can't test this right now. But maybe you can experiment it.
Pascal Thivent
Thanks Pascal.I did a local test. unfortunately I do see that the generated hibernate.cfg.xml lists mapping based on the schema I point to.So far I couldn't find any indication that the maven plugin can supports pointing to a directory where the mapping files are.my next step would be listing the mapping files manually :-(
naor