Hibernate Tips: How to create a database setup script based on entity mappings
Take your skills to the next level!
The Persistence Hub is the place to be for every Java developer. It gives you access to all my premium video courses, monthly Java Persistence News, monthly coding problems, and regular expert sessions.
Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question you like me to answer, please leave a comment below.
Question:
I want to use a database setup script instead of Hibernate’s schema generation. What’s the easiest way to create this script for an existing entity model?
Solution:
JPA 2.1 introduced a schema generation features which can setup your database or export the generated commands to a file. You just have to set the following configuration parameters in your persistence.xml file to activate it:
javax.persistence.schema-generation.scripts.action
Defines which scripts the persistence provider shall create. You can choose between none, create, drop-and-create, drop.
A script target needs to be defined for each script to be created.
javax.persistence.schema-generation.scripts.create-target
Defines the target location of the create script generated by the persistence provider as a file URL or a java.IO.Writer.
javax.persistence.schema-generation.scripts.drop-target
Defines the target location of the drop script generated by the persistence provider as a file URL or a java.IO.Writer.
Here you can see a persistence.xml configuration that generates a create and a drop script based on the entity mapping information
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="EFS2015-persistence-unit" transaction-type="JTA"> <description>Forge Persistence Unit</description> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/> <property name="javax.persistence.schema-generation.scripts.create-target" value="./create.sql"/> <property name="javax.persistence.schema-generation.scripts.drop-target" value="./drop.sql"/> </properties> </persistence-unit> </persistence>
Learn More:
You can use JPA’s schema generation feature also to setup and initialize databases based on the entity mapping information or a set of SQL scripts. I explain it in more detail in my post Standardized schema generation and data loading with JPA 2.1.
Hibernate Tips Book
Get more recipes like this one in my new book Hibernate Tips: More than 70 solutions to common Hibernate problems.
It gives you more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching, and statically and dynamically defined queries.
Hello, I see the script you are referring to but what is the command that needs to be run that will read the script and then create the sql?
Thank you,
–Brad
Hibernate creates the scripts based on your entity definition. In the next step, you can review, adapt and run the scripts using your database tools.