首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何配置hbm2java maven2插件为所有映射文件生成POJO

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

Stack Overflow用户
提问于 2010-05-31 03:43:34
回答 1查看 6.1K关注 0票数 3

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

代码语言:javascript
复制
<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是:

代码语言:javascript
复制
<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文件中,我有:

代码语言:javascript
复制
<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

EN

回答 1

Stack Overflow用户

发布于 2010-05-31 04:01:21

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

代码语言:javascript
复制
<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文件包含以下信息

代码语言:javascript
复制
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处理)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2940019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档