JPA 2.1: Schema generation properties

JPA 2.1 Schema generation properties

If you have some experience of Hibernate before, you must have used Hibernate hibernate.hbm2ddl.auto property to maintain the database schema for your project and used import.sql to initialize test data into the database.

Luckily this feature is standardized in JPA 2.1

JPA 2.1 added a series of properties for database schema maintenance and generation.

Maintain database schema

Let's create a simple Entity as example.

@Entity
@Table(name="POSTS")
public class Post implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="ID")
    private Long id;

    @Column(name="TITLE")
    private String title;

    @Column(name="BODY")
    private String body;

    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name="CREATED")
    private Date created;
}

A simple Post class, includes three properties, title, body, created, and additional id.

An example of persitence.xml.

 


 

 
    
 
  
 
   
    
     
     
     
     
     
     
     
    
  


 

 

javax.persistence.schema-generation.database.action defines the strategy will perform on database artifacts(tables, constraints etc). The value options are none, create, drop, create-and-drop.

javax.persistence.schema-generation.create-source specifies how to create these artifacts, the value could be script, metadata, script-then-metadata, metadata-then-script, which indicates the artifacts will be created from scripts or entity metadata. If script is specified, you can define another javax.persistence.schema-generation.create-script-source property to locate the script.

javax.persistence.schema-generation.drop-source and javax.persistence.schema-generation.drop-script-source are easy to understand, which define the strategy to drop the artifacts.

javax.persistence.sql-load-script-source will initialize some test data into the database.

The following are samples of the three scripts.

The content of create-script.sql.

CREATE TABLE POSTS("ID" INTEGER NOT NULL PRIMARY KEY, "TITLE" VARCHAR(255), "BODY" VARCHAR(2000), "CREATED" DATE)

The content of drop-script.sql.

DROP TABLE POSTS

The content of load-script.sql.

INSERT INTO POSTS("ID", "TITLE", "BODY", "CREATED") VALUES (1, 'First Post', 'Body of first post', '2013-11-27')
INSERT INTO POSTS("ID", "TITLE", "BODY", "CREATED") VALUES (2, 'Second Post', 'Body of second post', '2013-11-27')
INSERT INTO POSTS("ID", "TITLE", "BODY", "CREATED") VALUES (3, 'Third Post', 'Body of third post', '2013-11-27')

When you run this project on Glassfish 4, you will see the sample data is loaded successfully at runtime.

NOTE, you could notice we have not defined a DataSource in the persistence.xml. Java EE 7 specs added a series of default resources, including Jdbc, JMS etc. If there is no Jdbc DataSource specified, the application will search the default Jdbc DataSource via JNDI name java:/comp/DefaultDataSource, it should be provided in any Java EE certificated application servers.

In the above example we use scripts to manage the tables. If you prefer models(aka metadata) to manage the database schema, there is an example.

 


 

 
    
 
  
 
   
    
     
     
     
     
     
    
  


 

 

This example is similar with Hibernate drop-create strategy.

Generate database schema

JPA 2.1 provides capability to generate the existing database schema into external resource.

There is an example of the usage from EclipseLink project wiki.

Map properties = new HashMap();
properties.put("javax.persistence.database-product-name", "Oracle");
properties.put("javax.persistence.database-major-version", 12);
properties.put("javax.persistence.database-minor-version", 1);
properties.put("javax.persistence.schema-generation.scripts.action", "drop-and-create");
properties.put("javax.persistence.schema-generation.scripts.drop-target", "jpa21-generate-schema-no-connection-drop.jdbc");
properties.put("javax.persistence.schema-generation.scripts.create-target", "jpa21-generate-schema-no-connection-create.jdbc");

Persistence.generateSchema("default", properties);

Try it yourself

Summary

This feature is good, but it lack a equivalent action to Hibernate update strategy. And the naming of these properties is very bad, I have to research the JPA documentation to differentiate their usage.

The sample codes are hosted on my github.com account, check out and play it yourself.

https://github.com/hantsy/ee7-sandbox

你可能感兴趣的:(JSF,Glassfish,JavaEE7,JPA2.1)