Ada*_*dam 7 ant orm maven-2 hibernate hbm2java
我正忙着将现有项目从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>
Run Code Online (Sandbox Code Playgroud)
我在互联网上看了一下,有些人似乎这样做(我认为)使用Maven中的Ant和Maven插件中的其他人.我宁愿避免混合Ant和Maven.任何人都可以建议一种方法来执行此操作,以便拾取所有.hbm.xml文件并生成代码作为Maven代码生成构建阶段的一部分吗?
谢谢!
亚当.
Pas*_*ent 14
好吧,如果你不想混合Ant和Maven(这是IMO的一个好主意),就有Maven Hibernate3插件.它的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>
Run Code Online (Sandbox Code Playgroud)
编辑:该插件实际上是寻找.hbm.xml
中target/classes
生成Java源文件.因此,如果您将映射文件放入其中src/main/resources
,它们将在插件调用target/classes
的process-resources
阶段被复制,并且事情将正常工作.我刚刚使用以下示例项目对此进行了测试:
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>
Run Code Online (Sandbox Code Playgroud)
而且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>
Run Code Online (Sandbox Code Playgroud)
使用此配置,运行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;
}
}
Run Code Online (Sandbox Code Playgroud)
一切都按照描述运作.
归档时间: |
|
查看次数: |
9082 次 |
最近记录: |