Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to configure hibernate-tools with maven to generate hibernate.cfg.xml, *.hbm.xml, POJOs and DAOs
    text
    copied!<p>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?</p> <p>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 <code>hbm2java</code> goal won't work as I put *.hbm.xml files into the <code>src/main/resources/package/path/</code> folder but <code>hbm2cfgxml</code> specifies the mapping files only by table name, i.e.:</p> <pre><code>&lt;mapping resource="MyTableName.hbm.xml" /&gt; </code></pre> <p>So the big question is: how can I configure <code>hbm2cfgxml</code> so that hibernate.cfg.xml looks like below:</p> <pre><code>&lt;mapping resource="package/path/MyTableName.hbm.xml" /&gt; </code></pre> <p>My pom.xml looks like this at the moment:</p> <pre><code>&lt;plugin&gt; &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt; &lt;artifactId&gt;hibernate3-maven-plugin&lt;/artifactId&gt; &lt;version&gt;2.2&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;hbm2cfgxml&lt;/id&gt; &lt;phase&gt;generate-sources&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;hbm2cfgxml&lt;/goal&gt; &lt;/goals&gt; &lt;inherited&gt;false&lt;/inherited&gt; &lt;configuration&gt; &lt;components&gt; &lt;component&gt; &lt;name&gt;hbm2cfgxml&lt;/name&gt; &lt;implemetation&gt;jdbcconfiguration&lt;/implementation&gt; &lt;outputDirectory&gt;src/main/resources/&lt;/outputDirectory&gt; &lt;/component&gt; &lt;/components&gt; &lt;componentProperties&gt; &lt;packagename&gt;package.path&lt;/packageName&gt; &lt;configurationFile&gt;src/main/resources/hibernate.cfg.xml&lt;/configurationFile&gt; &lt;/componentProperties&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; </code></pre> <p>And then the second question: is there a way to tell maven to copy resources to the target folder before executing <code>hbm2java</code>? At the moment I'm using</p> <pre><code>mvn clean resources:resources generate-sources </code></pre> <p>for that, but there must be a better way.</p> <p>Thanks for any help.</p> <p><strong>Update:</strong></p> <p><strong>@Pascal:</strong> 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).</p> <p>I've deleted the file hibernate.cfg.xml, replaced it with database.properties and run the goals <code>hbm2cfgxml</code> and <code>hbm2hbmxml</code>. I also don't use the <code>outputDirectory</code> nor <code>configurationfile</code> in those goals anymore.</p> <p>As a result the files <code>hibernate.cfg.xml</code> and all <code>*.hbm.xml</code> are being generated into my target/hibernate3/generated-mappings/ folder, which is the default value. Then I updated the <code>hbm2java</code> goal with the following:</p> <pre><code>&lt;componentProperties&gt; &lt;packagename&gt;package.name&lt;/packagename&gt; &lt;configurationfile&gt;target/hibernate3/generated-mappings/hibernate.cfg.xml&lt;/configurationfile&gt; &lt;/componentProperties&gt; </code></pre> <p>But then I get the following:</p> <pre><code>[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 </code></pre> <p>How do I deal with that? Of course I could add:</p> <pre><code>&lt;outputDirectory&gt;src/main/resources/package/name&lt;/outputDirectory&gt; </code></pre> <p>to the <code>hbm2hbmxml</code> goal, but I think this is not the best approach, or is it? Is there a way to keep <strong>all</strong> the generated code and resources away from the <code>src/</code> folder?</p> <p>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 <code>hbm2dao</code> and packaging the project to be used as a generated persistence layer component from the business layer. Is this also what you meant?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload