If you like letting a framework write your database DDL while you sip coffee and hope for the best this short tutorial shows how to use Hibernate 5 SchemaExport to generate tables from JPA annotated classes. It is programmatic schema generation that keeps SQL fatigue to a minimum and still gives you control when you need it.
SchemaExport takes the mapping in your annotated entity classes and the Hibernate metadata model and produces DDL for your target database. You can emit SQL to a file, print it to standard out, or actually execute it against the database. It is ideal for initial schema creation in development or for reproducible builds in CI pipelines.
Add Hibernate core and the JDBC driver for your database to your Maven project. Keep your connection properties handy including the JDBC driver class and database credentials. Double check those credentials now so you do not get the delightful surprise of a failed connection later.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.x.y</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.x.x</version>
</dependency>
Annotate your classes with JPA annotations so the mapping represents the schema you want. Be sensible about column lengths and nullability so the database does not teach you harsh lessons later.
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue
private Long id;
@Column(length = 100, nullable = false)
private String name;
// getters and setters
}
Boot the ServiceRegistry, feed annotated classes into MetadataSources, and build a Metadata object. Then configure SchemaExport and run it. Here is an example that shows the typical flow in Java using Hibernate 5 APIs.
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySettings(properties)
.build();
MetadataSources sources = new MetadataSources(registry);
sources.addAnnotatedClass(Person.class);
Metadata metadata = sources.buildMetadata();
SchemaExport schemaExport = new SchemaExport();
schemaExport.setFormat(true);
schemaExport.setOutputFile("schema.sql");
schemaExport.create(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata);
Use different TargetType values depending on whether you want to run DDL against the database, write a script file, or just print SQL for review. Running with SCRIPT and STDOUT is a polite dry run that does not touch the live database.
Open your favorite database client and inspect the tables indexes and constraints. Run select queries and check for nullable columns that should not be nullable. If you find issues update the mappings rebuild Metadata and rerun SchemaExport until the schema behaves like you expected it to.
Use SchemaExport for programmatic schema generation when you want reproducible DDL and less manual SQL writing. Keep an iterative workflow where you build metadata, preview SQL, and then apply to a test database before touching production. Hibernate 5 SchemaExport is not magic but it is a very tidy assistant when you teach it well.
If you made it this far congratulations you now have a reliable path to let Hibernate generate DDL while you get to keep writing Java and pretend the database is not judging you.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.