[JAVA] Automatically generate jpa entity using Gradle

Background

Hibenate-Tools that works with plugins cannot be used with IDEs other than Eclipse. I don't think there is a good Gradle plugin, so I made it a task of gradle.

Execution environment

procedure

Added hibernate-tools to gradle dependencies Addendum: When using Spring-Boot, slf4j is caught in gradle test, so please comment it out or exclude it.

build.gradle


testRuntime group: 'org.hibernate', name: 'hibernate-tools', version: '5.3.6.Final'

Add task, change path and package according to environment.

build.gradle


task hbm2java{
    def basePackage = "com.hoge.entity.jpa.gen"
    def resourcesDir="$projectDir/src/main/resources"
    def srcDir="$projectDir/src/main/java"
    def preparedJdbcConfiguration = [
            propertyfile:       resourcesDir+"/hibernate.properties",
            revengfile:         resourcesDir+"/hibernate.reveng.xml",
            packagename:        basePackage
    ]
    
    doLast {
        project.ant {
            taskdef(name: "hibernatetool",
                    classname: "org.hibernate.tool.ant.HibernateToolTask",
                    classpath: configurations.testRuntime.asPath
            )
            hibernatetool(destdir: srcDir,
                    templatepath: 'templates'
            ) {
                jdbcconfiguration(preparedJdbcConfiguration)
                classpath{
                    pathelement( path: "config" )
                }
                hbm2java(jdk5: true,ejb3: true)
            }
        }
    }
}

Placement of hibernate.properties file

hibernate.properties


hibernate.connection.driver_class = com.mysql.cj.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/hoge_db?useSSL=false
hibernate.connection.username = user
hibernate.connection.password = passwd
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.MySQLDialect

Placement of Hibernate Reverse Engineering files

xml:hibernate.reveng.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering
    SYSTEM "http://hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd">

<hibernate-reverse-engineering>
    <schema-selection match-catalog="hoge_db" match-schema=".*" match-table="foo" />
    <schema-selection match-catalog="hoge_db" match-schema=".*" match-table="bar" />
</hibernate-reverse-engineering>

After that, execute Task on the console

$ gradle hbm2java

Spring Data JPA does not have an entity generator. I was wondering what everyone was doing, but I just had to use ant with gradle, that kind of thing.

Reference material

Recommended Posts

Automatically generate jpa entity using Gradle
Automatically generate DDL using [jpa-schema-gradle-plugin] plugin in Spring × Gradle
Generate DB documentation using SchemaSpy in Gradle
Let's automatically generate Mapper & Entity with MyBatis Generator!
Gradle settings for using JUnit 5
Using Docker from Java Gradle
I made a source that automatically generates JPA Entity class files
(Spring-data-jpa) How to get POJO (other than entity class) using JPA2.1