Database Schema Generator

Schema generator can generate database schema for a persistence unit, including entity tables, join tables, collection tables and Id generator (sequence or table generator). It can generate schema by JDBC or android built-in sqlite database.

Command Line

Command line supports JDBC only. 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
-jdbcUrl connectionUrl  -- jdbc connection string
-jdbcUser username      -- jdbc username
-jdbcPassword password  -- jdbc password
The jdbc properties can be specified in META-INF/persistence.xml or in the command line, and jdbc properties in command line override those in META-INF/persistence.xml.

If there is only one persistence unit in the persistence.xml, the -pu option can be omitted. The database schema should be empty without any tables before running the generator.

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:1.1.1'
	schemaGenerator 'com.cmobilecom:cmobilecom-jpa-processor:1.1.1'
		
	// 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 '-jdbcUrl', 'jdbc:mysql://localhost:3306/jpa_example_db?useSSL=false'
	args '-jdbcUser', 'jpa_example_user'
	args '-jdbcPassword', '123456'
}

Java Code (JDBC)

	Map<String, Object> properties = new HashMap<String, Object>();
	// JDBC properties
	properties.put(Constants.JDBC_USER, "test");
	properties.put(Constants.JDBC_PASSWORD, "123456");
	properties.put(Constants.JDBC_URL, "jdbc:mysql://localhost:3306/jpa_example_db?useSSL=false");
	
	PersistenceProviderResolver persistenceProviderResolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();
	List<PersistenceProvider> providers = persistenceProviderResolver.getPersistenceProviders();
	if (providers.size() == 0)
		throw new PersistenceException("no providers");
	
	// assume there is only one provider
	PersistenceProvider provider = providers.get(0);
	provider.generateSchema("persistenceUnitName", properties);

Android

Android code is the same as the java code (JDBC) above except that an android Context is needed to connect to android sqlite database.
	Map<String, Object> properties = new HashMap<String, Object>();
	Context context = ...  // application context or activity
	properties.put(Constants.ANDROID_CONTEXT, context);
	properties.put(Constants.ANDROID_CONNECTION_URL, "android:sqlite:jpa_example_db");
	
	PersistenceProviderResolver persistenceProviderResolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver();
	List<PersistenceProvider> providers = persistenceProviderResolver.getPersistenceProviders();
	if (providers.size() == 0)
		throw new PersistenceException("no providers");
	
	// assume there is only one provider
	PersistenceProvider provider = providers.get(0);
	provider.generateSchema("persistenceUnitName", properties);
The connection url starting with android:sqlite: tells Cmobilecom JPA to connect to the native android sqlite database.