Hibernate Tips: How to create a database setup script based on entity mappings
Create a database setup script based on entity mappings

Hibernate Tips: How to create a database setup script based on entity mappings

Hibernate Tips is a series of posts and a book in which I describe a quick and easy solution for common . 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?

Don’t want to read? You can watch it here!

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>

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.

Get it now as a paperback, ebook or PDF.

Read from the original source Here

Sir How to do this using Java Configuration without using Xml Configuration.

Like
Reply

To view or add a comment, sign in

More articles by Thorben Janssen

Others also viewed

Explore content categories