Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Generating Database Schemas with JPA 2.1

DZone's Guide to

Generating Database Schemas with JPA 2.1

· Database Zone
Free Resource

Whether you work in SQL Server Management Studio or Visual Studio, Redgate tools integrate with your existing infrastructure, enabling you to align DevOps for your applications with DevOps for your SQL Server databases. Discover true Database DevOps, brought to you in partnership with Redgate.

Prior JPA 2.1, most JPA providers (HibernateEclipseLinkOpenJPA) would have a proprietary way to generate the database schema (Data Definition Language scripts) from Entities. In the JPA 2.1 specification (JSR 338) there is a new section called 9.4 Schema Generation introducing an API and some properties to generate a database schema in a standard and portable way.

Properties in persistence.xml

In the history of JPA specification, we always had standard properties (prefixed with javax.persistence) and proprietary ones (eclipselink.xxx, hibernate.xxx, etc.). Each new release of JPA brought new standard properties and we had to wait for JPA 2.1 to deal with schema generation :

  • javax.persistence.schema-generation.database.action : for database action, values are “none“, “create“, “drop-and-create“, “drop“.
  • javax.persistence.schema-generation.scripts.action : for DDL scripts, values are “none“, “create“, “drop-and-create“, “drop“.
  • javax.persistence.schema-generation.scripts.create-target : name of the create table script
  • javax.persistence.schema-generation.scripts.drop-target : name of the drop table script

JPA 2.1 also brought a way to load data at startup. That’s a common need that we have: we usually want to have a database in a certain state with a certain number of rows, keys, foreign keys, reference tables and so on (good for integration testing for example). Before we had proprietary solutions such as the Hibernate import.sql facilities, or more traditional tools such as DBUnit or Benerator (for load testing). With JPA 2.1 we can also use the new standard property javax.persistence.sql-load-script-source.

Below is a persistence.xml file that generates a create (sampleCreate.ddl) and a drop (sampleDrop.ddl) script, but also executes a script at startup (the insert.sql script). Just add these properties in yourpersistence.xml, run the application, and you will see the magic happen :

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

  <persistence-unit name="samplePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>org.agoncal.sample.loadScriptSource.Book</class>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
      <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
      <property name="javax.persistence.schema-generation.scripts.create-target" value="sampleCreate.ddl"/>
      <property name="javax.persistence.schema-generation.scripts.drop-target" value="sampleDrop.ddl"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:sampleDB;create=true"/>
      <property name="javax.persistence.sql-load-script-source" value="insert.sql"/>
    </properties>
  </persistence-unit>

</persistence>

API to Generate the Schema

JPA 2.1 also brought a Java API which, based on the persistent unit name, can generate the schema. To make this happen, just take a Main class and invoke the generateSchema method passing the persistence unit name (here samplePU) :

public class Main {

  public static void main(String[] args) {
    Persistence.generateSchema("samplePU", null);
  }
}

References

It’s easier than you think to extend DevOps practices to SQL Server with Redgate tools. Discover how to introduce true Database DevOps, brought to you in partnership with Redgate

Topics:
java ,sql ,tips and tricks

Published at DZone with permission of Antonio Goncalves, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}