MavenのJavaソースコード生成のために、Hibernate
質問
Im忙しいのに変換する既存のプロジェクトからの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>
ご覧いただ周辺でのインターネットともなっていました。用Ant内Mavenおよびその他のMavenプラグイン.かな混合Ant、Maven.誰でもできるので提案をすることができます。hbm.xml ファイルは、コードかの一環として、Mavenコード生成を構築です。
よろしく!
Adam.
解決
あの Maven Hibernate3プラグイン だいたいミックスAnt、Mavenる、どちらが良いのかというこIMO).この 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 |-- main | |-- java | `-- resources | |-- Person.hbm.xml | `-- hibernate.cfg.xml `-- test `-- java
の pom.xml
ほぼ空でだけが含まれていますのプラグイン設定ることを示す一例といえる程度php,c#,dに依存性がある。の 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;
}
}
ものとして説明します。
他のヒント
パスカル、あなたの助けのためにもう一度感謝!あなたのソリューションは、うまく動作します。
これに取り組んでいる間、私が遭遇した他の物事のカップル。最初は、これはかなり大きなプロジェクトですので、私は、元のアリマルチディレクトリのビルドを反映するために、複数のMavenのモジュールに分割してきたという事実に関連します。生成されたクラスを含むモジュールは、実際に任意のデータベース・アクセスを行いませんので、hibernate.cfg.xmlファイルは必要はなく、この場合には、任意のDB接続情報を含むべきではありません。私は、このアウトを試みたのだが、プロパティタグのすべてに、パスカルが提供するファイルの削減バージョンで正常に動作します取り除かます。
これにより場所で、ビルドはコマンドラインからうまく働きました。私は、私は、Eclipseから実行したときに生成されたクラスを拾うために他のモジュールを説得できなかったかもしれませんしかし、試してみてください。当面、これまで私が持っている解決策は、設定/構成要素/コンポーネントの下POMに次の行を追加することである。
<outputDirectory>/src/main/java</outputDirectory>
このファイルは、他のモジュールのためにそれらを拾うことができます日食場所に生成することが強制されます。これが行われたら、コマンドラインでビルドを行い、その後、Eclipseは新しいファイルをピックアップするソースディレクトリの内容を更新することを要求しなければなりません。まだ、私はこれを処理するクリーンな方法を知りません....
あなたは相コンパイルに* .hbm.xmlを含める必要がある場合は、あなたのpom.xmlを編集し、次のコードを追加します:
<build>
<resources>
<resource>
<directory>source/com/qfund/orm/</directory>
<targetPath>com/qfund/orm/</targetPath>
<includes>
<include>*.hbm.xml</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/java/</directory>
<includes>
<include>*.xml</include>
<include>*.xsd</include>
<include>*.xslt</include>
<include>*.properties</include>
</includes>
</testResource>
</testResources>
</build>