Gradle plugin for generate schema or DDL scripts from JPA entities using JPA 2.1 schema generator. for Maven, see Maven Plugin.
Currently support EclipseLink (Reference Implementation) and Hibernate.
READ MY LIP; JPA DDL GENERATOR IS NOT SILVER BULLET
Sometimes (most times exactly :P) JPA will generate weird scripts so you SHOULD modify them properly.
Finally, I got some times, and 0.2 is here.
- Support generate without
persistence.xml
(like spring-data, spring-boot, ...) related #14 - Add support DataNucleus
- Changed default version of implementations.
- Eclipselink:
2.6.1
- Hibernate:
5.0.7.Final
- DataNucleus:
4.2.3
- Eclipselink:
- Added
properties
property. - Removed properties
namingStrategy
anddialect
cause Hibernate 4.x to 5.x is cataclysm. please useproperties
instead.
On 0.2.x, plugin required
- Java 1.7 or above, and
- Gradle 2.x. (developed on Gradle 2.5)
DataNucleus support is very very limited, and should so many buggy.
Put this to your build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'io.github.divinespear:jpa-schema-gradle-plugin:+'
// jdbc drivers also here
...
}
}
apply plugin: 'java'
apply plugin: 'jpa-schema-generate' // or 'io.github.divinespear.jpa-schema-generate'
sourceSets {
main {
// set output to same directories
// jpa implementations always scan classes using classpath that found persistence.xml
output.resourcesDir = output.classesDir
}
}
generateSchema {
// default options
// see SchemaGenerationConfig to all options
...
// if you want multiple output
targets {
targetName {
// same as default options
...
}
}
}
To generate schema, run
gradle generateSchema
or
./gradlew generateSchema
see also test cases Generate*Spec.groovy
, as examples.
You can change version using configurations
on buildscript
, like:
buildscript {
...
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.group == 'org.hibernate') {
details.useVersion '4.3.11.Final'
}
}
}
}
It should useful if you using Hibernate with Spring Framework, because Spring Framework not officially support Hibernate 5.x yet.
You MUST specify two options: vendor
and packageToScan
.
generateSchema {
vendor = 'hibernate' // 'eclipselink', 'hibernate', or 'datanucleus'.
// you can use class name too. (like 'org.hibernate.jpa.HibernatePersistenceProvider')
packageToScan = [ 'your.package.to.scan', ... ]
...
}
You MUST put scala-library
to dependencies
of buildscript
.
buildscript {
...
dependencies {
...
classpath "org.scala-lang:scala-library:${your_scala_version}"
...
}
}
Hibernate DOES NOT SUPPORT @GeneratedValue(strategy = GenerationType.SEQUENCE)
for DBMS dosen't support CREATE/DROP SEQUENCE
. WTF?! You should use @GeneratedValue
instead.
EclipseLink's Oracle{8,9,10,11}Platform
uses some type classes from Oracle's JDBC driver. you should have it in your dependency.
DataNucleus DOES NOT SUPPORT generate DDL without database connection.
Here is full list of parameters of generateSchema
.
name | type | description |
---|---|---|
skip |
boolean |
skip schema generation default value is |
format |
boolean |
generate as formatted default value is |
scanTestClasses |
boolean |
scan test classes default value is |
persistenceXml |
string |
location of persistence.xml fileNote: Hibernate DOES NOT SUPPORT custom location. ( default value is |
persistenceUnitName |
string |
unit name of persistence.xml default value is |
databaseAction |
string |
schema generation action for database support value is one of
default value is |
scriptAction |
string |
schema generation action for script support value is one of
default value is |
outputDirectory |
file |
output directory for generated ddl scripts REQUIRED for default value is |
createOutputFileName |
string |
generated create script name REQUIRED for default value is |
dropOutputFileName |
string |
generated drop script name REQUIRED for default value is |
createSourceMode |
string |
specifies whether the creation of database artifacts is to occur on the basis of the object/relational mappingmetadata, DDL script, or a combination of the two. support value is one of
default value is |
createSourceFile |
string |
create source file path. REQUIRED for |
dropSourceMode |
string |
specifies whether the dropping of database artifacts is to occur on the basis of the object/relational mappingmetadata, DDL script, or a combination of the two. support value is one of
default value is |
dropSourceFile |
file |
drop source file path. REQUIRED for |
jdbcDriver |
string |
jdbc driver class name default is declared class name in persistence xml. and Remember, |
jdbcUrl |
string |
jdbc connection url default is declared connection url in persistence xml. |
jdbcUser |
string |
jdbc connection username default is declared username in persistence xml. |
jdbcPassword |
string |
jdbc connection password default is declared password in persistence xml. If your account has no password (especially local file-base, like Apache Derby, H2, etc...), it can be omitted. |
databaseProductName |
string |
database product name for emulate database connection. this should useful for script-only action.
|
databaseMajorVersion |
int |
database major version for emulate database connection. this should useful for script-only action.
|
databaseMinorVersion |
int |
database minor version for emulate database connection. this should useful for script-only action.
|
lineSeparator |
string |
line separator for generated schema file. support value is one of default value is system property |
properties |
java.util.Map |
JPA vendor specific properties. |
vendor |
string |
JPA vendor name or class name of vendor's PersistenceProvider implemention.vendor name is one of
REQUIRED for project without |
packageToScan |
java.util.List |
list of package name for scan entity classes REQUIRED for project without |
It's just groovy map, so you can config like this:
generateSchema {
...
// global properties
properties = [
'hibernate.dialect': 'org.hibernate.dialect.MySQL5InnoDBDialect',
...
]
// you can set target-specific too.
...
}
It's about databaseProductName
property. If not listed below, will work as basic standard SQL.
databaseMajorVersion
and databaseMinorVersion
is not required.
Oracle12
= Oracle 12cOracle11
= Oracle 11gOracle10
: Oracle 10gOracle9
: Oracle 9iOracle
: Oracle with default compatibilityMicrosoft SQL Server
DB2
MySQL
PostgreSQL
SQL Anywhere
Sybase SQL Server
Adaptive Server Enterprise
= SybasePointbase
Informix Dynamic Server
Firebird
ingres
Apache Derby
H2
HSQL Database Engine
Some products uses different dialect by databaseMajorVersion
and/or databaseMinorVersion
.
You can override using hibernate.dialect
property.
CUBRID
org.hibernate.dialect.CUBRIDDialect
= all version
HSQL Database Engine
org.hibernate.dialect.HSQLDialect
= all version
H2
org.hibernate.dialect.H2Dialect
= all version
MySQL
org.hibernate.dialect.MySQL5Dialect
= 5.xorg.hibernate.dialect.MySQLDialect
= 4.x or beloworg.hibernate.dialect.MySQLMyISAMDialect
org.hibernate.dialect.MySQLInnoDBDialect
org.hibernate.dialect.MySQL5InnoDBDialect
org.hibernate.dialect.MySQL57InnoDBDialect
PostgreSQL
org.hibernate.dialect.PostgreSQL94Dialect
= 9.4 or aboveorg.hibernate.dialect.PostgreSQL92Dialect
= 9.2 or aboveorg.hibernate.dialect.PostgreSQL9Dialect
= 9.xorg.hibernate.dialect.PostgreSQL82Dialect
= 8.2 or aboveorg.hibernate.dialect.PostgreSQL81Dialect
= 8.1 or below
Apache Derby
org.hibernate.dialect.DerbyTenSevenDialect
= 10.7 or aboveorg.hibernate.dialect.DerbyTenSixDialect
= 10.6org.hibernate.dialect.DerbyTenFiveDialect
= 10.5org.hibernate.dialect.DerbyDialect
= 10.4 or below
ingres
org.hibernate.dialect.Ingres10Dialect
= 10.xorg.hibernate.dialect.Ingres9Dialect
= 9.2 or aboveorg.hibernate.dialect.IngresDialect
= 9.1 or below
Microsoft SQL Server
org.hibernate.dialect.SQLServer2012Dialect
= 11.xorg.hibernate.dialect.SQLServer2008Dialect
= 10.xorg.hibernate.dialect.SQLServer2005Dialect
= 9.xorg.hibernate.dialect.SQLServerDialect
= 8.x or below
Sybase SQL Server
org.hibernate.dialect.SybaseASE15Dialect
= all versionorg.hibernate.dialect.SybaseASE17Dialect
Adaptive Server Enterprise
= SybaseAdaptive Server Anywhere
org.hibernate.dialect.SybaseAnywhereDialect
= all version
Informix Dynamic Server
org.hibernate.dialect.InformixDialect
= all version
DB2 UDB for AS/390
org.hibernate.dialect.DB2390Dialect
DB2 UDB for AS/400
org.hibernate.dialect.DB2400Dialect
= all version
- start with
DB2/
org.hibernate.dialect.DB2Dialect
= all version
Oracle
org.hibernate.dialect.Oracle12cDialect
= 12.xorg.hibernate.dialect.Oracle10gDialect
= 11.x, 10.xorg.hibernate.dialect.Oracle9iDialect
= 9.xorg.hibernate.dialect.Oracle8iDialect
= 8.x or below
Firebird
org.hibernate.dialect.FirebirdDialect
= all version
Source Copyright © 2013 Sin-young "Divinespear" Kang. Distributed under the Apache License, Version 2.0.