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

发布于 2024-09-03 16:46:08 字数 1879 浏览 6 评论 0原文

我正在尝试将我的 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任务的文件集?

谢谢, 瑙尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2024-09-10 16:46:08

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

<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.xmlsrc/main/resources中(因此将被复制到target/classes中以供hbm2java处理)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文