本文介绍了用于 Hibernate 的 Maven Java 源代码生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙着将现有项目从 Ant 构建转换为使用 Maven 的项目.此构建的一部分包括使用 hibernate hbm2java 工具将 .hbm.

<hibernatetool destdir="${src.generated}"><配置><fileset dir="${src.config}"><include name="**/*.hbm.

我在互联网上环顾四周,有些人似乎(我认为)使用 Maven 中的 Ant 执行此操作,而其他人则使用 Maven 插件执行此操作.我宁愿避免混合使用 Ant 和 Maven.任何人都可以提出一种方法来执行此操作,以便提取所有 .hbm.

谢谢!

亚当.

解决方案

嗯,有 Maven Hibernate3 Plugin 如果您不想混合使用 Ant 和 Maven(在 IMO 中这是一个好主意).它有一个 hbm2java 目标默认绑定到 generate-sources 阶段.有关更多详细信息,请参阅 Mojo 的网站,但插件的设置可能如下所示:

 <groupId>org.codehaus.mojo</groupId><artifactId>hibernate3-maven-plugin</artifactId><version>2.2</version><执行><执行><phase>generate-sources</phase><目标><goal>hbm2java</goal></目标></执行></执行><配置><组件><组件><name>hbm2java</name><实施>配置</实施><outputDirectory>target/generated-sources/hibernate3</outputDirectory></组件></组件><组件属性><drop>true</drop><jdk5>真</jdk5><配置文件>/src/main/resources/hibernate.cfg.

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

maven-hibernate3-testcase|-- pom.

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

<property name="show_sql">false</property><!-- 映射文件--><mapping resource="Person.hbm.

Person.hbm. 如下所示:

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

$ cat target/generated-sources/hibernate3/Person.java//默认包//由 Hibernate Tools 3.2.2.GA 于 2009 年 12 月 14 日下午 2:19:22 生成/*** 由 hbm2java 生成的人*/公共类 Person 实现 java.io.Serializable {私有整数 ID;私有字符串名称;公共人(){}公共人(字符串名称){this.name = 名称;}公共 int getId() {返回 this.id;}公共无效 setId(int id) {this.id = id;}公共字符串 getName() {返回 this.name;}公共无效集名称(字符串名称){this.name = 名称;}}

一切正常.

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.

<target name="dbcodegen" depends="cleangen"
        description="Generate Java source from Hibernate 

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.

Thanks!

Adam.

解决方案

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.

EDIT: The plugin actually looks for .hbm. 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.

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

<?

And Person.hbm. looks as follow:

<?

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.

这篇关于用于 Hibernate 的 Maven Java 源代码生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:59