This is the directory structure of the project (maven2 is used):
pom.xml
/src
/main
/java
Abc.java
/resources
hibernate.cfg.xml
database.properties
/META-INF
persistence.xml
/test
/java
AbcTest.java
/resources
database.properties
This is the content of hibernate.cfg.xml:
<hibernate-configuration>
<session-factory name="java:hibernate/SessionFactory">
<property name="hibernate.archive.autodetection">true</property>
</session-factory>
</hibernate-configuration>
This is what I have in persistence.xml:
<persistence>
<persistence-unit name="abc">
<jta-data-source>java:/abcDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
And this is my Abc.java file:
import javax.persistence.*;
@Entity
public class Abc {
@Id private int id;
}
After running mvn clean hibernate3:hbm2ddl I’m getting this output:
18:45:55,770 INFO org.hibernate.tool.hbm2ddl.SchemaExport - writing
generated schema to file: ../target/hibernate3/sql/schema.ddl
18:45:55,770 INFO org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete
[INFO] ————————————————————————————————————
[INFO] BUILD SUCCESSFUL
File schema.ddl is created, and it’s empty. Why? And besides that, what is wrong in my configuration files? Since when I’m trying to run unit tests with PersistenceContext injections they fail with NullPointerException. Looks like there is some problem in the configuration. Can’t find any manual online…
PS. There are two problems, I found them already. The first one is here (extra prefix should be removed):
<property name="archive.autodetection">true</property>
The second one is more interesting. When I’m running mvn hibernate3:hbm2ddl after compilation it works (because it has .class files to work with). Otherwise the schema is empty.. How to instruct this plugin to compile java classes beforehand?
Indeed. So I’ll skip this one.
Not possible (but the other way around would be, i.e. running the plugin after
compile, as we’ll see).The fact is that the Hibernate3 Maven Plugin, which predates annotations, has been initially designed to deal with
hbm.xmlmapping files. And that’s whyhibernate3:hbm2ddlinvokes the execution of the lifecycle phaseprocess-resourcesprior to executing itself.When using annotation instead of XML files for the mappings, the goal would indeed have to run after the
compilephase (theprocess-classesphase would be a natural candidate) but that’s not the current behavior ofhibernate3:hbm2ddl.So you’ll have to run
compilebefore invoking the goal:The other option would be to bind the
hibernate3:hbm2ddlon the build lifecycle, e.g. onprocess-classes:And then just run
process-classesto trigger the plugin: