naor
naor

Reputation: 31

How do I configure the hbm2java maven2 plugin to generate POJOs for all mapping files

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

Upvotes: 3

Views: 6640

Answers (1)

Pascal Thivent
Pascal Thivent

Reputation: 570545

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).

Upvotes: 3

Related Questions