Id like to define my objects then use hibernate to generate my ddl from this. But it seems like the only real workflow with hibernate is to generate a datbase schema and then reverse engineer it. Am I wanting to do something that makes no sense?
Is there a simple workflow to generate a database schema from classes with hibernate mappings?
811 Views Asked by Andrew Bucknell AtThere are 2 best solutions below

But it seems like the only real workflow with hibernate is to generate a database schema and then reverse engineer it.
No, absolutely not. Historically, Hibernate supports several approaches:
Top down: You start with the object model, then create mapping metatdata either with XML files or annotations and then generate the schema using Hibernate's
hbm2ddl
tool.Bottom up: You start with an existing database and generate mappings and Java classes (or just annotated Java classes) from the database schema using
hbm2hbmxml
andhbm2java
.Middle out: You start be writing Hibernate XML mappings and generate DDL and Java classes using
hbm2ddl
andhbm2java
respectively.Meet in the middle: You have an existing Java model and an existing database. Hibernate tooling can't help much here, you'll very likely have to write mappings by hand and do some refactoring of the database, or of the Java code, or use some kind of bridge. This is the worst situation.
So, as we saw, Hibernate supports several workflows and provides tooling for them. And in your case, you're looking for the tool called hbm2ddl
(also known as SchemaExport
, which is the name of the class implementing it). There are several ways to use it:
- You can run
SchemaExport
programmatically. - You can enable automatic export of the schema at
SessionFactory
creation time by setting the propertyhibernate.hbm2ddl.auto
to an appropriate value - You can use the
<hbm2ddl>
Ant task
References
- Hibernate Core Reference Guide
- Hibernate Tools Reference Guide
Yes there is a property
hibernate.hbm2ddl.auto
. The documentation says:There are multiple ways to define this property, depending on how you configure your
SessionFactory
. The hibernate way is to simply add: