Enterprise Java
JPA persistence.xml SQL script definitions
You can define and link to SQL scripts in a JPA persistence context definition that will be executed at runtime. There are standardized properties to define scripts how to create the schema, bulk-load data and drop the schema, respectively:
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="prod" transaction-type="JTA"> <properties> <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> <property name="javax.persistence.schema-generation.create-script-source" value="create-schema.sql" /> <property name="javax.persistence.schema-generation.sql-load-script-source" value="load-data.sql" /> <property name="javax.persistence.schema-generation.drop-script-source" value="drop-schema.sql" /> </properties> </persistence-unit> </persistence>
The SQL files are expected to reside in the classpath.
This post was reposted from my newsletter issue 004
Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: JPA persistence.xml SQL script definitions Opinions expressed by Java Code Geeks contributors are their own. |