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 😱) JPA will generate weird scripts, so you SHOULD modify them properly.
See Releases for more information...
Counterpart for Gradle 6.x
- Required JDK 8 or above.
- Required Gradle 6.0 or above. (for support Java 13 or above)
Counterpart Gradle 4.10 to 5.x
- Required JDK 8 or above.
- Required Gradle 4.10 or above. (for support spring-boot plugin version 2.0+)
Groovy
plugins {
id 'io.github.divinespear.jpa-schema-generate' version '0.4.0'
}
generateSchema {
// default options
// see SchemaGenerationConfig to all options
...
// if you want multiple output
targets {
targetName {
// same as default options
...
}
}
}
Kotlin
plugins {
id("io.github.divinespear.jpa-schema-generate") version("0.4.0")
}
generateSchema {
// default options
// see SchemaGenerationConfig to all options
...
// if you want multiple output
targets {
create("targetName") {
// same as default options
...
}
}
}
To generate schema, run
gradle generateSchema
or
./gradlew generateSchema
see also functional test cases as examples.
You MUST specify two options: vendor
and packageToScan
.
Groovy
generateSchema {
vendor = 'hibernate' // 'eclipselink', 'hibernate', or 'hibernate+spring'.
// you can use class name too. (like 'org.hibernate.jpa.HibernatePersistenceProvider')
packageToScan = [ 'your.package.to.scan', /* more */ ]
// ...omitted...
}
Kotlin
generateSchema {
vendor = "hibernate" // 'eclipselink', 'hibernate', or 'hibernate+spring'.
// you can use class name too. (like 'org.hibernate.jpa.HibernatePersistenceProvider')
packageToScan = setOf("your.package.to.scan", /* more */)
// ...omitted...
}
Since 0.3.4, you can add dependencies for plugin with configuration generateSchema
.
Groovy
// no need to add 'generateSchema' into configurations block.
dependencies {
// ...omitted...
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// only need to load java.time converter from spring-data-jpa on schema generation
generateSchema 'org.threeten:threetenbp:1.3.6'
}
generateSchema {
// ...omitted...
packageToScan = [
// load java.time converter from spring-data-jpa
'org.springframework.data.jpa.convert.threeten',
'your.package.to.scan',
// more...
]
}
Kotlin
// no need to add 'generateSchema' into configurations block.
dependencies {
// ...omitted...
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
// only need to load java.time converter from spring-data-jpa on schema generation
generateSchema("org.threeten:threetenbp:1.3.6")
}
generateSchema {
// ...omitted...
packageToScan = setOf(
// load java.time converter from spring-data-jpa
"org.springframework.data.jpa.convert.threeten",
"your.package.to.scan",
// more...
)
}
- EclipseLink 2.6 on Java 12 or higher will not work. embedded ASM library cannot read class files.
- EclipseLink's
Oracle{8,9,10,11}Platform
uses some type classes from Oracle's JDBC driver. you should have it in your dependency.
-
After 5.2, just use
hibernate-core
insteadhibernate-entitymanager
, it is merged. -
Naming strategy property is
- 4.x:
hibernate.ejb.naming_strategy
- 5.x:
hibernate.physical_naming_strategy
/hibernate.implicit_naming_strategy
- 4.x:
-
For select dialect without
persistence.xml
, do one of- set
hibernate.dialect
onproperties
- set
databaseProductName
,databaseMajorVersion
, and/ordatabaseMinorVersion
for determine dialect.
- set
vendor
should behibernate+spring
, others are same as hibernate withoutpersistence.xml
- use
io.spring.dependency-management
for version management.- You can change hibernate version with
hibernate.version
property.
- You can change hibernate version with
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 implementation.vendor name is one of
REQUIRED for project without |
packageToScan |
java.util.Set |
list of package name for scan entity classes REQUIRED for project without |
It's just map, so you can config like this
Groovy
generateSchema {
// global properties
properties = [
'hibernate.dialect': 'org.hibernate.dialect.MySQL5InnoDBDialect',
// more...
]
// you can set target-specific too.
}
Kotlin
generateSchema {
// global properties
properties = mapOf(
"hibernate.dialect" to "org.hibernate.dialect.MySQL5InnoDBDialect",
// more...
)
// 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
For other versions, select tag to your version.
Source Copyright © 2013-2021 Sinyoung "Divinespear" Kang (divinespear at gmail dot com). Distributed under the Apache License, Version 2.0.