Cmobilecom JPA 2.0.RC1 Developer Guide

5. Schema Generation

Schema generation creates database schema objects and/or scripts for a persistence unit, including entity tables, join tables, collection tables, id generator sequences and tables. Schema objects can be created (or dropped) from metadata (persistence unit metamodel) and/or user-provided scripts based on standard schema generation properties.

Auto Generation

To enable schema generation, add one or more standard schema generation properties in META-INF/persistence.xml, or pass them when creating EntityManagerFactory. For example,

create database schema objects from metadata:

	<property name="javax.persistence.schema-generation.database.action" value="create"/>
or
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put(Constants.SCHEMA_GENERATION_DB_ACTION, "create");
	EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu", properties);
Create database schema objects from metadata and script:
	<property name="javax.persistence.schema-generation.database.action" value="create"/>
	<property name="javax.persistence.schema-generation.create-source" value="metadata-then-script"/>
	<property name="javax.persistence.schema-generation.create-script-source" value="file:///path/create.sql"/>
Create drop and create scripts from metadata:
	<property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
	<property name="javax.persistence.schema-generation.drop-script-target" value="file:///path/drop.sql"/>
	<property name="javax.persistence.schema-generation.create-script-target" value="file:///path/create.sql"/>
Create database schema objects from script and load initial data:
	<property name="javax.persistence.schema-generation.database.action" value="create"/>
	<property name="javax.persistence.schema-generation.create-source" value="script"/>
	<property name="javax.persistence.schema-generation.create-script-source" value="file:///path/create.sql"/>
	<property name="javax.persistence.sql-load-script-source" value="file:///path/load.sql"/>
For a complete list of JPA standard properties, see Query Hints

Command Line

To generate database schema for a persistence unit:
	java -classpath classpath com.cmobilecom.jpa.processor.SchemaGenerator options
The following files must be in the classpath: options:
-pu puName                   -- persistence unit name
-propertyName propertyValue  -- standard property name/value
The properties can be specified in META-INF/persistence.xml or in the command line, and properties in command line override those in META-INF/persistence.xml.

Gradle build example: generate schema for mysql database. Suppose managed classes and META-INF/persistence.xml are in main source set.

configurations {
	schemaGenerator
}
	
dependencies {	
	schemaGenerator 'com.cmobilecom:cmobilecom-jpa-jdbc:2.0.RC1'
	schemaGenerator 'com.cmobilecom:cmobilecom-jpa-processor:2.0.RC1'
		
	// jdbc driver
	schemaGenerator 'mysql:mysql-connector-java:6.0.6'
	
	// logging SQL statements
	schemaGenerator 'ch.qos.logback:logback-classic:1.2.3'
	schemaGenerator 'ch.qos.logback:logback-core:1.2.3'
}

task schemaGenerator(type: JavaExec) {
	classpath = configurations.schemaGenerator + sourceSets.main.output
	main 'com.cmobilecom.jpa.processor.SchemaGenerator'
	args '-pu', 'jpa_example'
	args '-javax.persistence.jdbc.url', 'jdbc:mysql://localhost:3306/jpa_example_db'
	args '-javax.persistence.jdbc.user', 'jpa_example_user'
	args '-javax.persistence.jdbc.password', '123456'
	args '-javax.persistence.schema-generation.database.action', 'drop-and-create'
}

Java Code

	Map<String, Object> properties = new HashMap<String, Object>();
	
	// jdbc
	properties.put(Constants.JDBC_USER, "jpa_example_user");
	properties.put(Constants.JDBC_PASSWORD, "123456");
	properties.put(Constants.JDBC_URL, "jdbc:mysql://localhost:3306/jpa_example_db");
	
	// android
	Context context = ...  // application context or activity
	properties.put(Constants.ANDROID_CONTEXT, context);
	properties.put(Constants.ANDROID_CONNECTION_URL, "android:sqlite:jpa_example_db");
	
	properties.put(Constants.SCHEMA_GENERATION_DB_ACTION, "drop-and-create");
	
	PersistenceProviderResolver persistenceProviderResolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();
	List<PersistenceProvider> providers = persistenceProviderResolver.getPersistenceProviders();
	
	for (PersistenceProvider provider : providers) {
		boolean generated = provider.generateSchema("persistenceUnitName", properties);
		if (generated)
			return;
	}
		
	throw new PersistenceException("JPA provider not found for the persistence unit");
Managed Type EnhancerPersistence UnitFrames / No Frames