Hibernate 的 Maven Java 源代码生成

发布于 2024-08-14 20:21:47 字数 774 浏览 8 评论 0原文

我正忙于将现有项目从 Ant 构建转换为使用 Maven 构建的项目。此构建的一部分包括使用 hibernate hbm2java 工具将 .hbm.xml 文件集合转换为 Java。以下是用于执行此操作的 Ant 脚本的片段:

<target name="dbcodegen" depends="cleangen" 
        description="Generate Java source from Hibernate XML">
  <hibernatetool destdir="${src.generated}">
    <configuration>   
      <fileset dir="${src.config}">
        <include name="**/*.hbm.xml"/>
      </fileset>
    </configuration>   
    <hbm2java jdk5="true"/>
  </hibernatetool>   
</target>

我在互联网上浏览了一下,有些人似乎(我认为)使用 Maven 中的 Ant 来执行此操作,而其他人则使用 Maven 插件。我宁愿避免混合 Ant 和 Maven。任何人都可以建议一种方法来执行此操作,以便选取所有 .hbm.xml 文件并在 Maven 代码生成构建阶段进行代码生成吗?

谢谢!

亚当.

I´m busy converting an existing project from an Ant build to one using Maven. Part of this build includes using the hibernate hbm2java tool to convert a collection of .hbm.xml files into Java. Here's a snippet of the Ant script used to do this:

<target name="dbcodegen" depends="cleangen" 
        description="Generate Java source from Hibernate XML">
  <hibernatetool destdir="${src.generated}">
    <configuration>   
      <fileset dir="${src.config}">
        <include name="**/*.hbm.xml"/>
      </fileset>
    </configuration>   
    <hbm2java jdk5="true"/>
  </hibernatetool>   
</target>

I've had a look around on the internet and some people seem to do this (I think) using Ant within Maven and others with the Maven plugin. I'd prefer to avoid mixing Ant and Maven. Can anyone suggest a way to do this so that all of the .hbm.xml files are picked up and the code generation takes place as part of the Maven code generation build phase?

Thanks!

Adam.

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

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

发布评论

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

评论(3

尐偏执 2024-08-21 20:21:47

好吧,如果您不这样做,可以使用 Maven Hibernate3 插件不想混合 Ant 和 Maven(我认为这是一个好主意)。它有一个 hbm2java 目标默认绑定到 generate-sources 阶段。请参阅 Mojo 网站了解更多详细信息,但插件的设置可能如下所示:

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

编辑: 该插件实际上在 .hbm.xml 中查找 .hbm.xml code>target/classes 生成java源文件。因此,如果您将映射文件放入 src/main/resources 中,它们将在 process-resources 阶段复制到 target/classes 中这是由插件调用的,一切都会正常工作。我刚刚使用以下示例项目对此进行了测试:

maven-hibernate3-testcase
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   `-- resources
    |       |-- Person.hbm.xml
    |       `-- hibernate.cfg.xml
    `-- test
        `-- java

pom.xml 几乎是空的,它只包含上面看到的插件配置和 junit 依赖项。 hibernate.cfg.xml 包含:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="connection.url">jdbc:derby://localhost:1527/mydatabase</property>
    <property name="connection.username">app</property>
    <property name="connection.password">app</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.DerbyDialect</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">false</property>

    <!-- Mapping files -->
    <mapping resource="Person.hbm.xml" />
  </session-factory>
</hibernate-configuration>

Person.hbm.xml 如下所示:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

  <class name="Person" table="person">
    <id name="id" type="int">
      <generator class="increment" />
    </id>

    <property name="name" column="cname" type="string" />
  </class>

</hibernate-mapping>

使用此配置,运行 mvn install 会生成 Person.java 如下所示:

$ cat target/generated-sources/hibernate3/Person.java 
// default package
// Generated Dec 14, 2009 2:19:22 PM by Hibernate Tools 3.2.2.GA



/**
 * Person generated by hbm2java
 */
public class Person  implements java.io.Serializable {


     private int id;
     private String name;

    public Person() {
    }

    public Person(String name) {
       this.name = name;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }




}

一切都按描述进行。

Well, there is the Maven Hibernate3 Plugin if you don't want to mix Ant and Maven (which is a good idea here IMO). It has a hbm2java goal which is bound by default to the generate-sources phase. Refer to the website of the Mojo for more details but the setup of the plugin might looks like something like this:

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

EDIT: The plugin actually looks for .hbm.xml in target/classes to generate the java source files. So, if you put your mapping files in src/main/resources, they will get copied into target/classes during the process-resources phase which is invoked by the plugin and things will just work. I've just tested this with the following sample project:

maven-hibernate3-testcase
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   `-- resources
    |       |-- Person.hbm.xml
    |       `-- hibernate.cfg.xml
    `-- test
        `-- java

The pom.xml is almost empty, it just contains the plugin configuration seen above and a junit dependency. The hibernate.cfg.xml contains:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="connection.url">jdbc:derby://localhost:1527/mydatabase</property>
    <property name="connection.username">app</property>
    <property name="connection.password">app</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.DerbyDialect</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">false</property>

    <!-- Mapping files -->
    <mapping resource="Person.hbm.xml" />
  </session-factory>
</hibernate-configuration>

And Person.hbm.xml looks as follow:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

  <class name="Person" table="person">
    <id name="id" type="int">
      <generator class="increment" />
    </id>

    <property name="name" column="cname" type="string" />
  </class>

</hibernate-mapping>

With this configuration, running mvn install generates Person.java as shown below:

$ cat target/generated-sources/hibernate3/Person.java 
// default package
// Generated Dec 14, 2009 2:19:22 PM by Hibernate Tools 3.2.2.GA



/**
 * Person generated by hbm2java
 */
public class Person  implements java.io.Serializable {


     private int id;
     private String name;

    public Person() {
    }

    public Person(String name) {
       this.name = name;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }




}

Everything works as described.

吲‖鸣 2024-08-21 20:21:47

帕斯卡,再次感谢您的帮助!您的解决方案效果很好。

我在从事这项工作时遇到了其他一些事情。第一个涉及到这是一个相当大的项目,因此我将其拆分为多个 Maven 模块以镜像原始的 ant 多目录构建。包含生成的类的模块实际上并不执行任何数据库访问,因此 hibernate.cfg.xml 文件不需要(在这种情况下也不应该)包含任何数据库连接信息。我已经尝试过了,它与 Pascal 提供的文件的缩减版本一起工作得很好,删除了所有属性标签。

完成此操作后,从命令行构建就可以正常工作了。然而,尽我所能,我无法说服其他模块在从 Eclipse 运行时选择生成的类。目前,我对此的解决方案是将以下行添加到配置/组件/组件下的 POM 中:

<outputDirectory>/src/main/java</outputDirectory>

这会强制在 Eclipse 可以为其他模块拾取它们的位置生成文件。完成此操作后,您必须在命令行上进行构建,然后请求 Eclipse 刷新源目录的内容以获取新文件。到目前为止,我不知道有更干净的方法来处理这个问题......

Pascal, thanks again for your help! Your solution works well.

A couple of other things I encountered while working on this. The first relates to the fact that this is a fairly big project and so I've split it into multiple Maven modules to mirror the original ant multi-directory build. The module that contains the generated classes doesn't actually do any database access and so the hibernate.cfg.xml file need not, and in this case should not, contain any DB connection information. I've tried this out and it works just fine with a cut down version of the file provided by Pascal, with all of the properties tags removed.

With this in place, the build worked fine from the command line. However, try as I might, I couldn't persuade the other modules to pick up the generated classes when run from Eclipse. For the time being, the solution I have to this is to add the following line into the POM under configuration/components/component:

<outputDirectory>/src/main/java</outputDirectory>

This forces the files to be generated in a place that eclipse can pick them up for the other modules. Once this is done you must do a build on the command line and then request that Eclipse refresh the contents of the source directory to pick up the new files. As yet, I don't know of a cleaner way to handle this....

嗼ふ静 2024-08-21 20:21:47

如果需要在编译阶段包含 *.hbm.xml;编辑 pom.xml 并添加以下代码:

<build>
                <resources>
            <resource>
                <directory>source/com/qfund/orm/</directory>
                <targetPath>com/qfund/orm/</targetPath>
                <includes>
                    <include>*.hbm.xml</include>
                </includes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/java/</directory>
                <includes>
                    <include>*.xml</include>
                    <include>*.xsd</include>
                    <include>*.xslt</include>
                    <include>*.properties</include>
                </includes>
            </testResource>
        </testResources>
</build>

If you need include *.hbm.xml on the phase compile; edit your pom.xml and add the next code:

<build>
                <resources>
            <resource>
                <directory>source/com/qfund/orm/</directory>
                <targetPath>com/qfund/orm/</targetPath>
                <includes>
                    <include>*.hbm.xml</include>
                </includes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/java/</directory>
                <includes>
                    <include>*.xml</include>
                    <include>*.xsd</include>
                    <include>*.xslt</include>
                    <include>*.properties</include>
                </includes>
            </testResource>
        </testResources>
</build>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文