14

自動生成されたhibernate.cfg.xmlファイルの.hbm.xmlファイルをパッケージパスでマッピングする前にMavenを強制する方法を教えてもらえますか?

私の一般的な考え方は、mavenを介してhibernate-toolsを使用して、アプリケーションの永続性レイヤーを生成したいということです。したがって、hibernate.cfg.xmlが必要であり、次にすべてのmy_table_names.hbm.xmlが必要であり、最後にPOJOが生成されます。それでも、 *。hbm.xmlファイルをフォルダーhbm2javaに入れると、目標は機能しませんが、マッピングファイルをテーブル名のみで指定します。src/main/resources/package/path/hbm2cfgxml

<mapping resource="MyTableName.hbm.xml" />

hbm2cfgxmlしたがって、大きな問題は、hibernate.cfg.xmlが次のようになるように構成するにはどうすればよいかということです。

<mapping resource="package/path/MyTableName.hbm.xml" />

私のpom.xmlは、現時点では次のようになっています。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <id>hbm2cfgxml</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>hbm2cfgxml</goal>
            </goals>
            <inherited>false</inherited>
            <configuration>
                <components>
                    <component>
                        <name>hbm2cfgxml</name>
                        <implemetation>jdbcconfiguration</implementation>
                        <outputDirectory>src/main/resources/</outputDirectory>
                    </component>
                </components>
                <componentProperties>
                    <packagename>package.path</packageName>
                    <configurationFile>src/main/resources/hibernate.cfg.xml</configurationFile>
                </componentProperties>
            </configuration>
        </execution>
    </executions>
</plugin>

そして2番目の質問:実行する前にリソースをターゲットフォルダーにコピーするようにMavenに指示する方法はありますhbm2javaか?現時点で使用しています

mvn clean resources:resources generate-sources

そのためですが、もっと良い方法があるはずです。

助けてくれてありがとう。

アップデート:

@Pascal:ご協力いただきありがとうございます。マッピングへのパスは現在正常に機能していますが、以前は何が悪かったのかわかりません。おそらく、データベース構成の読み取り中にhibernate.cfg.xmlへの書き込みに問題があります(ファイルは更新されますが)。

ファイルhibernate.cfg.xmlを削除し、それをdatabase.propertiesに置き換えて、ゴールhbm2cfgxmlとを実行しましたhbm2hbmxml。私はまた、それらの目標でoutputDirectorynorをもう使用しません。configurationfile

その結果、ファイルhibernate.cfg.xmlとすべて*.hbm.xmlがデフォルト値であるtarget / hibernate3/generated-mappings/フォルダーに生成されます。hbm2java次に、次のように目標を更新しました。

<componentProperties>
    <packagename>package.name</packagename>
    <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
</componentProperties>

しかし、私は次のようになります。

[INFO] --- hibernate3-maven-plugin:2.2:hbm2java (hbm2java) @ project.persistence ---
[INFO] using configuration task.
[INFO] Configuration XML file loaded: file:/C:/Documents%20and%20Settings/mmm/workspace/project.persistence/target/hibernate3/generated-mappings/hibernate.cfg.xml
12:15:17,484  INFO org.hibernate.cfg.Configuration - configuring from url: file:/C:/Documents%20and%20Settings/mmm/workspace/project.persistence/target/hibernate3/generated-mappings/hibernate.cfg.xml
12:15:19,046  INFO org.hibernate.cfg.Configuration - Reading mappings from resource : package.name/Messages.hbm.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java (hbm2java) on project project.persistence: Execution hbm2java of goal org.codehaus.mojo:hibernate3-maven-plugin:2.2:hbm2java failed: resource: package/name/Messages.hbm.xml not found

どうすれば対処できますか?もちろん、私は追加することができます:

<outputDirectory>src/main/resources/package/name</outputDirectory>

目標に向かってhbm2hbmxmlいますが、これは最善のアプローチではないと思いますか?生成されたすべてのコードとリソースをsrc/フォルダーから遠ざける方法はありますか?

このアプローチの目標は、src / main /javaまたは/resourcesフォルダーにソースを生成することではなく、生成されたコードをターゲットフォルダーに保持することだと思います。私はこの観点に概ね同意しているので、最終的にhbm2daoはプロジェクトを実行してパッケージ化し、ビジネスレイヤーから生成された永続性レイヤーコンポーネントとして使用することを継続したいと思います。これもあなたが意味したことですか?

4

3 に答える 3

9

hibernate.cfg.xmlが以下のようになるようにhbm2cfgxmlを構成するにはどうすればよいですか(...)

を使用しているプロジェクトがhbm2cfgxmlあり、<mapping resource="..."/>エントリはへのパスにパッケージ名を反映していますhbm.xml。ですから、明らかにあなたの側に何か問題があります。ここにいくつかの意見があります:

  • 私はフェーズにバインドhbm2cfgxmlgenerate-resourcesます、あなたはソースを生成していません
  • 私はファイルを生成しませんsrc/main/resourcesが、で生成しますtarget/classses(生成されたものをソースツリーに配置するのはなぜですか、cleanそれをクリーンアップする必要があります)。
  • タイプミスがありますが、そうではありconfigurationfileませんconfigurationFile...
  • なぜあなたは<configurationfile>の構成に地獄を持っているのhbm2cfgxmlですか?ここで生成します...削除します。

更新:データベースに接続するために必要な情報は、 (そのファイルを削除する)ではなく(プロパティsrc/main/resources/database.propertiesのデフォルト値)に配置する必要があります。サンプルの下:propertyfilesrc/main/resources/hibernate.cfg.xmldatabase.properties

hibernate.connection.driver_class=org.apache.derby.jdbc.ClientDriver
hibernate.connection.url=jdbc:derby://localhost:1527//home/pascal/Projects/derbyDBs/EMPLDB
hibernate.connection.username=APP
hibernate.connection.password=APP
hibernate.dialect=org.hibernate.dialect.DerbyDialect

そして、私が言ったように、src/main/resources/hibernate.cfg.xmlファイルを削除します、あなたはそれを生成したいと思います。

hbm2javaを実行する前にリソースをターゲットフォルダーにコピーするようにmavenに指示する方法はありますか?(...)

hbm2java目標は、ライフサイクルフェーズのプロセスの実行を呼び出します-それ自体を実行する前にリソース(ドキュメントから)。これがデフォルトの動作であり、hibernate3:hbm2javaまたはにバインドされているgenerate-sources 場合に発生します。 hbm2java

于 2010-05-16T14:23:27.177 に答える
2

OK、mavenにhbm.xmlファイルを/ target / classes / package / nameフォルダーに配置するように強制することで問題を修正したので、最後に私のpomは次のようになります。

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>hibernate3-maven-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>hbm2cfgxml</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>hbm2cfgxml</goal>
                    </goals>
                    <inherited>false</inherited>
                    <configuration>
                        <components>
                            <component>
                                <name>hbm2cfgxml</name>
                                <implementation>jdbcconfiguration</implementation>
                            </component>
                        </components>
                        <componentProperties>
                            <packagename>package.name</packagename>
                        </componentProperties>
                    </configuration>
                </execution>
                <execution>
                    <id>hbm2hbmxml</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>hbm2hbmxml</goal>
                    </goals>
                    <inherited>false</inherited>
                    <configuration>
                        <components>
                            <component>
                                <name>hbm2hbmxml</name>
                                <outputDirectory>target/classes</outputDirectory>
                            </component>
                        </components>
                        <componentProperties>
                            <packagename>package.name</packagename>
                        </componentProperties>
                    </configuration>
                </execution>
                <execution>
                    <id>hbm2java</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>hbm2java</goal>
                    </goals>
                    <inherited>false</inherited>
                    <configuration>
                        <components>
                            <component>
                                <name>hbm2java</name>
                                <implementation>configuration</implementation>
                            </component>
                        </components>
                        <componentProperties>
                            <packagename>package.name</packagename>
                            <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
                        </componentProperties>
                    </configuration>
                </execution>
                <execution>
                    <id>hbm2dao</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>hbm2dao</goal>
                    </goals>
                    <inherited>false</inherited>
                    <configuration>
                        <components>
                            <component>
                                <name>hbm2dao</name>
                                <implementation>configuration</implementation>
                            </component>
                        </components>
                        <componentProperties>
                            <packagename>package.name</packagename>
                            <configurationfile>target/hibernate3/generated-mappings/hibernate.cfg.xml</configurationfile>
                        </componentProperties>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>8.4-701.jdbc3</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

そしてそれは大丈夫です。他の投稿で見られるように、初期のビルドフェーズでは、これらのファイルをtarget / hibernate3 /generated-mappings(デフォルトで生成される)からtarget / classes / package / name(hibernate-toolsの外観)にhbm.xmlコピーする必要があります。彼らのために)、しかし私の場合、彼らはそうではありません(これは私が何か間違ったことをしていることを示しています)。ですから、私が間違っているのかもしれないことを知っている人がいたら、教えてください。そうでなければ、それで十分です。

動作していないことが1つあります。それは、生成されたPOJOとDAOでパッケージ名が使用されていないことです。しかし、ここで別のスレッドを作成しまし

更新:わかりました、今私はついにそれを手に入れました。パッケージ名が欠落している問題は、hbm2hbmxml目標の構成にありました。そこにpackagenameを持つcomponentPropertiesがないため、生成されたものは完全に分類されたクラス名を見逃していました。上記のpomを更新しましたが、正常に動作します。ただし、ファイルをtarget/classesフォルダーに明示的にコピーすることに関する問題は依然として当てはまります。hbm.xmlhbm.xml

于 2010-05-17T13:39:30.863 に答える
1

hibernate3-maven-pluginを使用せずにMavenでHibernateツールを使用する方法の例については、この投稿を確認してください。アイデアは、MavenでHibernateツールのAntタスクを実行することです。このアプローチにより、プロセスを完全に制御できます。

于 2016-02-13T10:25:22.500 に答える