3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Gradleを使ってjpaのエンティティを自動生成

Last updated at Posted at 2018-11-02

#経緯
Eclipse以外のIDEではプラグインで動くHibenate-Toolsが使えない。
Gradleのプラグインにいい感じのものがなさそうなので、gradleのTask化した。

#実行環境

  • gradle 4.10.2
  • MySQL 5.7
  • OpenJDK 11.0.1
  • Hibernate-tools 5.3.6.Final

#手順

gradleのdependenciesにhibernate-toolsを追加
追記:Spring-Bootを使う場合 gradle test でslf4jが引っかかるのでコメントアウトしてしまうかexcludeしてください。

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

taskの追加、pathやpackageは環境に合わせて変更。

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)
            }
        }
    }
}

hibernate.propertiesファイルの配置

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

Hibernate Reverse Engineering ファイルの配置

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>

あとはコンソールでTaskの実行

$ gradle hbm2java

Spring Data JPAにはエンティティージェネレータがない。
皆どうしてるのか疑問だったが、gradleでantを使えばいいだけだったとか、そういうの。

#参考資料

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?