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

mmm picture mmm · May 16, 2010 · Viewed 32.2k times · Source

can any one tell me how to force maven to precede mapping .hbm.xml files in the automatically generated hibernate.cfg.xml file with package path?

My general idea is, I'd like to use hibernate-tools via maven to generate the persistence layer for my application. So, I need the hibernate.cfg.xml, then all my_table_names.hbm.xml and at the end the POJO's generated. Yet, the hbm2java goal won't work as I put *.hbm.xml files into the src/main/resources/package/path/ folder but hbm2cfgxml specifies the mapping files only by table name, i.e.:

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

So the big question is: how can I configure hbm2cfgxml so that hibernate.cfg.xml looks like below:

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

My pom.xml looks like this at the moment:

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

And then the second question: is there a way to tell maven to copy resources to the target folder before executing hbm2java? At the moment I'm using

mvn clean resources:resources generate-sources

for that, but there must be a better way.

Thanks for any help.

Update:

@Pascal: Thank you for your help. The path to mappings works fine now, I don't know what was wrong before, though. Maybe there is some issue with writing to hibernate.cfg.xml while reading database config from it (though the file gets updated).

I've deleted the file hibernate.cfg.xml, replaced it with database.properties and run the goals hbm2cfgxml and hbm2hbmxml. I also don't use the outputDirectory nor configurationfile in those goals anymore.

As a result the files hibernate.cfg.xml and all *.hbm.xml are being generated into my target/hibernate3/generated-mappings/ folder, which is the default value. Then I updated the hbm2java goal with the following:

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

But then I get the following:

[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

How do I deal with that? Of course I could add:

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

to the hbm2hbmxml goal, but I think this is not the best approach, or is it? Is there a way to keep all the generated code and resources away from the src/ folder?

I assume, the goal of this approach is not to generate any sources into my src/main/java or /resources folder, but to keep the generated code in the target folder. As I generally agree with this point of view, I'd like to continue with that eventually executing hbm2dao and packaging the project to be used as a generated persistence layer component from the business layer. Is this also what you meant?

Answer

Pascal Thivent picture Pascal Thivent · May 16, 2010

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.