This plugin intend to ease hibernatetools through gradle. It offer three tasks:
- hibernate-config: to generate hibernate config files
- hbm2java: to generate java classes
- hbm2dao: to generate DAO classes
Inside your build.gradle file add this:
plugins {
id "org.hibernate.gradle.tools" version "1.2.4"
}
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.org.hibernate.gradle.tools:hibernatetools-gradle-plugin:1.2.4"
}
}
apply plugin: "org.hibernate.gradle.tools"
This project has been updated to use hibernate-tools-5.3.4.Final but 5.2.x is still supported. To override the hibernate-tools version add:
reveng ('org.hibernate:hibernate-tools:5.2.11.Final') {
force = true
}
to your build.gradle's main dependencies
section (not the buildscript
/dependencies
section).
Now you have only to configure the database, by using database section inside build.gradle file. Example:
If using domain,
database{
basePackage = "com.project.database.model"
url = "jdbc:mysql://myDB.domain.fr"
}
If using host,
database{
basePackage = "com.project.database.model"
url = "jdbc:mysql://host"
dbName = "myDB"
}
This plugin allow to specify multiple catalog using:
import org.hibernate.gradle.tools.*
database{
catalog = [ "catalog1": new Schema("schemaY", [".*"])), "catalog2": new Schema("schemaX", [".*"]) ]
basePackage = "org.foo.bar"
}
This plugin allow to specify multiple schema using:
database{
catalog = [ "catalog1": new Schema("schemaX", ["tableY"]), "catalog2": new Schema("schemaY", [".*"]) ]
basePackage = "org.foo.bar"
}
This plugin allow to specify multiple tables using:
database{
catalog = [ "catalog1": new Schema("schemaX", ["tableY","tableZ"]), "catalog2": new Schema("schemaY", [".*"]) ]
basePackage = "org.foo.bar"
}
The symbol '.*' means any characters repeated 0 on n times. In given example class will be mapped from foo and bar tables and also any table where name start with other
The config files hibernate-tools needs are generated by the hibernate-config
task, alternatively you can provide them by setting these properties:
database{
configXml = "/tmp/hibernate.cfg.xml" // <<---- path to config file for hibernate-tools 5.2.x
hibernatePropertiesFile = "/tmp/hibernate.properties" // <<---- path to config properties file for hibernate-tools 5.3.x
revEngXml = "/tmp/hibernate.reveng.xml" // <<---- path to reveng file
}
by default database section are defined as:
class Database {
def catalog = [".*":new Schema(".*", ".*")] // Schema parameter are: schema pattern name, table pattern name
String user = ""
String password = ""
String url = "jdbc:mysql://127.0.0.1"
Integer port = 3306
String dbName = ""
String driver = "com.mysql.jdbc.Driver"
String dialect = "org.hibernate.dialect.MySQLDialect"
String basePackage = "com.project.database.model"
}
dialect and driver to use are located on hibernate
Once the plugin is declared and database connection defined to get java classes you need only to run this command:
$ gradle hbm2dao
This plugin provides three gradle target:
- hbm2java
- hbm2dao
- hibernate-config
enjoy