用于 Hibernate 的 Maven Java 源代码生成 [英] Maven Java Source Code Generation for Hibernate

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

问题描述

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

<hibernatetool destdir="${src.generated}"><配置><fileset dir="${src.config}"><include name="**/*.hbm.xml"/></文件集></配置><hbm2java jdk5="true"/></hibernatetool></目标>

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

谢谢!

亚当.

解决方案

嗯,有 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.xml</configurationfile></componentProperties></配置></插件>

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

<前>maven-hibernate3-testcase|-- pom.xml`-- 源代码|-- 主要||-- 爪哇|`--资源||-- Person.hbm.xml|`-- hibernate.cfg.xml`-- 测试`--java

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

<休眠配置><会话工厂><!-- 数据库连接设置--><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连接池(使用内置)--><property name="connection.pool_size">1</property><!-- SQL方言--><property name="dialect">org.hibernate.dialect.DerbyDialect</property><!-- 将所有执行的 SQL 回显到标准输出 --><property name="show_sql">false</property><!-- 映射文件--><mapping resource="Person.hbm.xml"/></session-factory></hibernate-configuration>

Person.hbm.xml 如下所示:

使用此配置,运行 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.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.

解决方案

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆