有谁能告诉我如何强制maven在自动生成的带有包路径的hibernate.cfg.xml文件中映射hibernate.cfg.xml文件之前呢?
我的总体想法是,我希望通过maven使用hibernate工具为我的应用程序生成持久层。所以,我需要hibernate.cfg.xml,然后是所有的my_table_names.hbm.xml,最后生成POJO。然而,hbm2java目标不能工作,因为我将*.hbm.xml文件放入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>然后是第二个问题:在执行hbm2java之前,是否有一种方法可以告诉maven将资源复制到目标文件夹?我现在用的是
mvn clean resources:resources generate-sources但一定有更好的办法。
谢谢你的帮助。
更新:
@Pascal:,谢谢您的帮助。到映射的路径现在运行良好,但我不知道之前发生了什么问题。在从hibernate.cfg.xml读取数据库配置时(尽管文件会更新),可能存在一些问题。
我删除了文件hibernate.cfg.xml,用database.properties替换了它,并运行了goals hbm2cfgxml和hbm2hbmxml。我也不再在这些目标中使用outputDirectory或configurationfile。
因此,文件hibernate.cfg.xml和所有*.hbm.xml将被生成到我的目标/hibernate3 3/ generated -*.hbm.xml/文件夹中,这是默认值。然后,我用以下内容更新了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并将项目打包为从业务层生成的持久性层组件。这也是你的意思吗?
发布于 2010-05-16 14:23:27
如何配置hbm2cfgxml使hibernate.cfg.xml看起来如下(.)
我有一个使用hbm2cfgxml的项目,<mapping resource="..."/>条目确实反映了hbm.xml路径中的packagename。所以很明显你这边出了点问题。以下是几点评论:
generate-resources阶段绑定hbm2cfgxml,您不是在生成源代码,而是在src/main/resources中生成文件,而是在target/classses中生成文件(为什么要将生成的东西放在源代码树中,您需要一个clean来清理它)。H 214H 115有一个错误,它是configurationfile,不是configurationFile而是…H 218H 119为什么在<>d21的配置中有<configurationfile>?你想在这里生成它..。我会把它移走的。更新:--您应该将连接到数据库所需的信息放在src/main/resources/database.properties ( propertyfile属性的默认值)中,而不是在src/main/resources/hibernate.cfg.xml (删除该文件)中。在示例database.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文件,您想要生成它。
是否有一种方法可以告诉maven在执行hbm2java之前将资源复制到目标文件夹?(.)
hbm2java目标调用生命周期阶段流程的执行--在执行自身之前(从文档中)。因此,这是默认行为,如果hibernate3:hbm2java或generate-sources 绑定到 hbm2java,则该行为将发生。
发布于 2010-05-17 13:39:30
好的,我通过强制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>而且工作正常。正如我在其他文章中看到的那样,在一些早期构建阶段,这些hbm.xml文件应该从目标/hibernate 3/生成映射(在默认情况下生成的映射)复制到目标/类/包/名称(hibernate-工具在其中查找它们),但在我的示例中,它们没有被复制(这表明我做错了什么事情)。所以如果有人知道我做错了什么,请告诉我。否则就足够了。
有一件事不起作用:在生成的POJO和DAO中没有使用包名:但是我为这个here创建了另一个线程。
更新: ok,现在我终于得到它了。缺少包名的问题是在hbm2hbmxml目标的配置中。我错过了带有packagename的componentProperties,因此生成的hbm.xml忽略了完全分类的类名。我更新了上面的pom,现在它很好用。但是,有关将hbm.xml文件显式复制到目标/类文件夹的问题仍然存在。
https://stackoverflow.com/questions/2843949
复制相似问题