如何配置hbm2java maven2插件以为所有映射文件生成POJO

时间:2010-05-30 19:43:34

标签: hibernate maven-2 hbm2java

我正在尝试将我的ant构建迁移到maven2。在我的build.xml中,我通过以下方式调用hbm2java:

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

我的hibernate.cfg.xml是:

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

在我的maven2 POM文件中:

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

但是在执行mvn hibernate3:hbm2java时,我看到没有文件生成,除非它们都列在hibernate.cfg.xml中。 有没有办法在maven配置中指定类似于ant任务的文件集?

感谢, NAOR

1 个答案:

答案 0 :(得分:3)

我不确定这是唯一的方法,但我会首先使用hbm2cfgxml生成hibernate.cfg.xml配置文件,其中包含<mapping resource="..."/>条目,然后是hbm2java目标生成POJO。下面是作为构建的一部分执行此操作的配置:

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

src/main/database.properties文件包含以下信息

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

此设置假设您的.hbm.xml位于src/main/resources(因此将被target/classes复制到hbm2java进行处理。