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

3

14

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?

Cards answered 16/5, 2010 at 13:35 Comment(0)
T
9

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.

Trug answered 16/5, 2010 at 14:23 Comment(1)
Thanks for your remarks Pascal. I moved the execution to generate-resources, corrected the typo. 1. How should I configure the user and password for hibernate-tools if not by hibernate.cfg.xml (or hibernate.properties)? The idea here is, hibernate.cfg.xml is being read, and updated if any changes in the data model have been made. Is there any more-optimal approach? All other options force me to maintain the same configuration in 2 separate files with different syntax. Regarding my second question, also thanks for your answer. The main question regarding the path/to/mappings is still open.Cards
C
3

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.

Cards answered 17/5, 2010 at 13:39 Comment(0)
T
1

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.

Temperate answered 13/2, 2016 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.