Reworked project-documentation: moved to maven-thymeleaf-skin
[hibernate4-maven-plugin] / src / site / xhtml / pitfalls.xhtml
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3  <head>
4  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5  </head>
6  <body>
7   <header><h1>Known Pitfalls (FAQ)</h1></header>
8   <h2>Annotated classes in dependencies are not found.</h2>
9   <p>
10   hibernate-maven-plugin by default scans dependencies in the scope
11   <code>compile</code>. You can configure it to scan dependencies in other
12   scopes as well. But it scans only direct dependencies. Transitive
13   dependencies are not scanned for annotated classes. If some of your
14   annotated classes are hidden in a transitive dependency, you can simply
15   add that dependency explicitly.
16   </p>
17   <h2>hibernate-maven-plugin always needs a database-connection</h2>
18   <p>
19   The default-configuration exports the created schema to the configured
20   database.
21   Therefore, it needs a valid database-connection and fails, if none is
22   available.
23   If you do not need to export the created schema to a database,
24   you can set the property <code>hibernate.schema.export</code> to
25   <code>false</code>.
26   This can be achieved with the command-line parameter
27   <code>-Dhibernate.schema.export=false</code> or with the following
28   configuration:
29   </p>
30   <pre class="prettyprint linenums lang-html">
31 &lt;configuration&gt;
32   &lt;export&gt;false&lt;/export&gt;
33 &lt;/configuration&gt;</pre>
34   <p>
35   But even when no database is to be created, hibernate always needs to know
36   the dialect. Hence, the plugin will still fail, if this parameter is also
37   missing!
38   </p>
39   <h2>Dependency for driver-class XYZ is missing</h2>
40   <p>
41   One regular problem is the scope of the jdbc-driver-dependency.
42   It is very unlikely, that this dependency is needed at compile-time.
43   So a tidy maven-developer would usually scope it for <code>runtime</code>.
44   </p>
45   <p>
46   But this will break the execution of the <code>hibernate-maven-plugin</code>.
47   Since it will not be able to see the needed dependency, it will fail with
48   an error-message like:
49   </p>
50   <pre class="prettyprint">
51 [INFO] Gathered hibernate-configuration (turn on debugging for details):
52 [INFO]   hibernate.connection.username = sa
53 [INFO]   hibernate.connection.password = 
54 [INFO]   hibernate.dialect = org.hibernate.dialect.H2Dialect
55 [INFO]   hibernate.connection.url = jdbc:h2:file:./db
56 [INFO]   hibernate.hbm2dll.create_namespaces = false
57 [INFO]   hibernate.connection.driver_class = org.h2.Driver
58 [INFO]   hibernate.format_sql = true
59 [INFO] HHH000412: Hibernate Core {5.0.2.Final}
60 [INFO] HHH000206: hibernate.properties not found
61 [INFO] HHH000021: Bytecode provider name : javassist
62 [INFO] Adding /home/kai/project/target/classes to the list of roots to scan...
63 [INFO] Adding dependencies from scope compile to the list of roots to scan
64 [INFO] Adding dependencies from scope org.hibernate:hibernate-core:jar:4.3.0.Final to the list of roots to scan
65 [INFO] Adding annotated resource: de.juplo.tests.SimplestMavenHib4Test
66 [INFO] ------------------------------------------------------------------------
67 [INFO] BUILD FAILURE
68 [INFO] ------------------------------------------------------------------------
69 [INFO] Total time: 1.760s
70 [INFO] Finished at: Mon Mar 07 19:06:54 CET 2016
71 [INFO] Final Memory: 11M/215M
72 [INFO] ------------------------------------------------------------------------
73 [ERROR] Failed to execute goal de.juplo:hibernate-maven-plugin:${project.version}:drop (default) on project hibernate4-properties-test: Could not open the JDBC-connection: Unable to load class [org.h2.Driver]: Could not load requested class : org.h2.Driver -&gt; [Help 1]
74 [ERROR] 
75 [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
76 [ERROR] Re-run Maven using the -X switch to enable full debug logging.
77 [ERROR] 
78 [ERROR] For more information about the errors and possible solutions, please read the following articles:
79 [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException</pre>
80   <p>
81   A quick workaround for this error would be, to delete the runtime-constraint
82   for the jdbc-driver-dependency.
83   </p>
84   <p>
85   A much cleaner way is, to (additionally) ad the dependency, to the
86   plugin-definition:
87   </p>
88   <pre class="prettyprint linenums lang-html">
89 &lt;plugin&gt;
90   &lt;groupId&gt;de.juplo&lt;/groupId&gt;
91   &lt;artifactId&gt;hibernate-maven-plugin&lt;/artifactId&gt;
92   &lt;version&gt;${project.version}&lt;/version&gt;
93   &lt;executions&gt;
94     &lt;execution&gt;
95       &lt;goals&gt;
96         &lt;goal&gt;drop&lt;/goal&gt;
97         &lt;goal&gt;create&lt;/goal&gt;
98       &lt;/goals&gt;
99     &lt;/execution&gt;
100   &lt;/executions&gt;
101   &lt;dependencies&gt;
102   &lt;dependency&gt;
103     &lt;groupId&gt;org.hsqldb&lt;/groupId&gt;
104     &lt;artifactId&gt;hsqldb&lt;/artifactId&gt;
105     &lt;version&gt;2.2.8&lt;/version&gt;
106   &lt;/dependency&gt;
107   &lt;/dependencies&gt;
108 &lt;/plugin&gt;</pre>
109   <p>
110   This is also the best way, if you use a different jdbc-driver for
111   testing, than in production.
112   Because otherwise, this dependency will unnecessarily bloat the
113   runtime-dependencies of your project.
114   </p>
115   <h2 id="fails">DBUnit fails after execution of hibernate was skipped because nothing has changed</h2>
116   <p>
117   If hibernate-maven-plugin skips its excecution, this may lead to errors in
118   other plugins.
119   For example, when importing sample-data in the automatically created database
120   with the help of the <a href="http://mojo.codehaus.org/dbunit-maven-plugin/">dbunit-plugin</a>,
121   the <code>CLEAN_INSERT</code>-operation may fail because of foreign-key-constraints,
122   if the database was not recreated, because the hibernate-maven-plugin has
123   skipped its excecution.
124   </p>
125   <p>
126   A quick fix to this problem is, to <a href="./force.html">force</a>
127   hibernate-maven-plugin to export the schema every time it is running.
128   But to recreate the database on every testrun may noticeable slow down your
129   development cycle, if you have to wait for slow IO.
130   </p>
131   <p>
132   To circumvent this problem, hibernate-maven-plugin signals a skipped
133   excecution by setting the  maven property <code>${hibernate.schema.skipped}</code> to
134   <code>true</code>.
135   You can configure other plugins to react on this signal.
136   For example, the dbunit-plugin can be configured to skip its excecution, if
137   hibernate-maven-plugin was skipped like this:
138   </p>
139   <pre class="prettyprint linenums lang-html">
140 &lt;plugin&gt;
141   &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
142   &lt;artifactId&gt;dbunit-maven-plugin&lt;/artifactId&gt;
143   &lt;configuration&gt;
144     &lt;skip&gt;${hibernate.schema.skipped}&lt;/skip&gt;
145   &lt;/configuration&gt;
146 &lt;/plugin&gt;</pre>
147   <h2>The database will not be recreated after a manual drop/clean</h2>
148   <p>
149   If one manually drops the database or removes the hsqldb-files, it will not
150   be recreated by the hibernate-maven-plugin, because it cannot detect, that
151   the database needs to be recreated.
152   This happens, because the plugin will not recreate the database if neither
153   the configuration nor the annotated classes have changed, because an
154   unnecessary drop-create-cycle might take a long time. The plugin will
155   report that like this:
156   </p>
157   <pre class="prettyprint">
158 [INFO] No modified annotated classes found and dialect unchanged.
159 [INFO] Skipping schema generation!</pre>
160   <p>
161   If one always uses <code>mvn clean</code> for cleanup, this will not happen.
162   Otherwise the recreation must be <a href="./force.html">forced</a>:
163   </p>
164   <pre class="prettyprint">
165 mvn hibernate:create -Dhibernate.schema.force=true</pre>
166   <h2>The hibernate:create goal is not executed, when tests are skipped</h2>
167   <p>
168   The hibernate-maven-plugin automatically skips its execution, when
169   <code>maven.test.skip</code> is set to <code>true</code>. If you need it to be always
170   executed, you can configure that explicitly like this:
171   </p>
172   <pre class="prettyprint linenums lang-html">
173 &lt;plugin&gt;
174   &lt;groupId&gt;de.juplo&lt;/groupId&gt;
175   &lt;artifactId&gt;hibernate-maven-plugin&lt;/artifactId&gt;
176   ...
177   &lt;configuration&gt;
178     &lt;skip&gt;false&lt;/skip&gt;
179   &lt;/configuration&gt;
180 &lt;/plugin&gt;</pre>
181   <p>
182   Background-information for this design-decission can be found on the extra
183   page <a href="./skip.html">To skip or not to skip: that is the question</a>...
184   </p>
185   <h2>I do not want my dependencies to be scanned for hibernate annotations</h2>
186   <p>
187     If you do not want your dependencies to be scanned for hibernate annotations,
188     you can pass <code>-Dhibernate.schema.scan.dependencies=none</code> to maven
189     or set <code>scanDependencies</code> to <code>none</code> in the configuration
190     of the plugin like this:
191   </p>
192   <pre class="prettyprint linenums lang-html">
193 &lt;plugin&gt;
194   &lt;groupId&gt;de.juplo&lt;/groupId&gt;
195   &lt;artifactId&gt;hibernate-maven-plugin&lt;/artifactId&gt;
196   ...
197   &lt;configuration&gt;
198     &lt;scanDependencies&gt;none&lt;/scanDependencies&gt;
199   &lt;/configuration&gt;
200 &lt;/plugin&gt;</pre>
201   <h2>No annotated classes found</h2>
202   <p>
203     If you are working under Windows and get the error-message
204     <code>No annotated classes found in directory C:\projects\X Y Z\path-to-project\target\classes</code>,
205     but you are really sure, that there are annotated classes in that
206     directory, you are expiriencing a bug, in the scannotation-library, that
207     was closed in version 1.1.0 of the hibernate-maven-plugin.
208   </p>
209   <p>
210     <strong>
211       You should consider to upgrade to the latest version of the plugin.
212     </strong>
213   </p>
214  </body>
215 </html>