Vâng, có Maven Hibernate3 Plugin nếu bạn không muốn kết hợp Ant và Maven (đây là ý tưởng hay ở đây IMO). Nó có một mục tiêu hbm2java
được ràng buộc theo mặc định cho giai đoạn generate-sources
. Tham khảo các trang web của Mojo để biết thêm chi tiết nhưng các thiết lập của sức Plugin trông giống như một cái gì đó như thế này:
<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: Plugin thực sự trông cho .hbm.xml
trong target/classes
để tạo ra các tập tin nguồn java. Vì vậy, nếu bạn đặt các tệp ánh xạ của mình trong src/main/resources
, chúng sẽ được sao chép vào target/classes
trong giai đoạn process-resources
được gọi bởi plugin và mọi thứ sẽ hoạt động. Tôi vừa mới thử nghiệm này với các dự án mẫu sau:
maven-hibernate3-testcase
|-- pom.xml
`-- src
|-- main
| |-- java
| `-- resources
| |-- Person.hbm.xml
| `-- hibernate.cfg.xml
`-- test
`-- java
Các pom.xml
là gần như trống rỗng, nó chỉ chứa các cấu hình Plugin thấy ở trên và một phụ thuộc junit. Các hibernate.cfg.xml
chứa:
<?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>
Và Person.hbm.xml
trông như sau:
<?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>
Với cấu hình này, chạy mvn install
tạo Person.java
như hình dưới đây:
$ 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;
}
}
Tất cả mọi thứ hoạt động như mô tả.
Quy ước maven3 mới dường như là $ {project.build.directory}/tạo/hibernate3/main/java hơn $ { project.build.directory}/generated/hibernate3 Tôi đang gặp khó khăn khi tìm tài liệu để hỗ trợ việc này. Plugin m2Eclipse mới dường như sử dụng quy ước mới này. Điều này đặc biệt rõ ràng khi sử dụng tính năng "Cấu hình dự án cập nhật". –