好吧,如果您不想混合 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>
编辑:.hbm.xml
插件实际上寻找target/classes
生成java源文件。因此,如果您将映射文件放入 中,它们将在插件调用的阶段src/main/resources
被复制到其中,并且一切正常。我刚刚使用以下示例项目对此进行了测试:target/classes
process-resources
maven-hibernate3-testcase
|-- pom.xml
`--src
|-- 主要
| |-- 爪哇
| `-- 资源
| |-- 人物.hbm.xml
| `--休眠.cfg.xml
`-- 测试
`--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;
}
}
一切都按描述工作。