How to configure hibernate-tools with maven to generate hibernate.cfg.xml, *.hbm.xml, POJOs and DAOs

泪湿孤枕 提交于 2019-11-30 05:11:38

how can I configure hbm2cfgxml so that hibernate.cfg.xml looks like below (...)

I have a project that is using hbm2cfgxml and the <mapping resource="..."/> entries do reflect the packagename in the path to the hbm.xml. So there is clearly something wrong on your side. Here are a few remarks:

  • I would bind hbm2cfgxml on the generate-resources phase, you're not generating sources
  • I wouldn't generate the file in src/main/resources but in target/classses (why do you put generated stuff in the source tree, you want a clean to clean it).
  • There is a typo, it's configurationfile, not configurationFile but...
  • Why the hell do you have a <configurationfile> in the configuration of hbm2cfgxml? You want to generate it here... I would remove it.

Update: You should put the informations required to connect to the database in src/main/resources/database.properties (that's the default value of the propertyfile property), not in src/main/resources/hibernate.cfg.xml (remove that file). Below a sample 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

And as I said, remove the src/main/resources/hibernate.cfg.xml file, you want to generate it.

is there a way to tell maven to copy resources to the target folder before executing hbm2java? (...)

The hbm2java goal Invokes the execution of the lifecycle phase process-resources prior to executing itself (from the documentation). So that's the default behavior and occurs with hibernate3:hbm2java or generate-sources if hbm2java is bound to it.

mmm

Ok, I fixed my problem by forcing maven to put the hbm.xml files into the /target/classes/package/name folder, so at the end my pom looks like this:

<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>

And it works ok. As fas as I could see in other posts, in some early build phases those hbm.xml files should be copied from target/hibernate3/generated-mappings (where they are generated by default) to target/classes/package/name (where hibernate-tools looks for them), but in my case they aren't (which indicates I'm doing something wrong). So if there is anyone out there knowing what it might be I'm doing wrong, please tell me. Otherwise It'll have to suffice.

There is one thing that isn't working: the package names aren't used in the generated POJOs and DAOs: but I created another thread for this here.

Update: ok, now I finally got it. The problem with missing package names was in the hbm2hbmxml goal's configuration. I missed the componentProperties with packagename there, so the generated hbm.xml missed the fully classified class names. I updated the above pom, now it works fine. The issue regarding explicit copying the hbm.xml files to the target/classes folder is still the case, though.

For an example about how to use Hibernate tools with maven, without using hibernate3-maven-plugin check this post. The idea is to run Hibernate tools Ant task with Maven. This approach gives you the full control over the process.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!