首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从gradle调用hbm2ddl

从gradle调用hbm2ddl
EN

Stack Overflow用户
提问于 2016-03-26 05:14:03
回答 1查看 1.6K关注 0票数 1

是否有gradle插件或任何其他方法可以调用hibernate-tools hbm2ddl任务来从带注释的类生成数据库模式,而不必在某个配置文件中列出所有实体(@Entity),而是在类路径中发现它们?

最好是Hibernate 5,但Hibernate 4也可以。

EN

回答 1

Stack Overflow用户

发布于 2016-03-26 23:11:48

我最终通过移动persistence.xml文件实现了这一点。

在我的场景中,我有一些库实体和一些应用程序实体,我想为它们生成一个模式。显然,我需要列出persistence.xml中的库实体,这很好,因为它们不会经常更改,但是为了让应用程序实体从类路径中被提取出来,而不是在持久性文件中列出它们,我必须确保类和persistence.xml文件都是由相同的类加载器加载的(我想是这样)。

这就是有效的方法。

库实体: MyCustomer、MyInvoice

应用程序实体: MyBook、MyBooking

/src/main/resources/META-INF/persistence.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

   <persistence-unit name="defaultPersistenceUnit">
        <!-- List the library classes only -->
        <class>net.mylibrary.entity.MyCustomer</class>
        <class>net.mylibrary.entity.MyInvoice</class>

      <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="myusr"/>
            <property name="hibernate.connection.password" value="mypwd"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/mydb"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
      </properties>
   </persistence-unit>
</persistence>

build.gradle:

代码语言:javascript
复制
apply plugin: 'war'
apply plugin: 'eclipse-wtp'

repositories {
    jcenter()
    mavenLocal()
}

dependencies {
    compile ... my project dependencies ...
}

configurations {
    hibtools
}

dependencies {
    hibtools 'net.mylibrary:MyEntities:1.0' // Library for MyCustomer and MyInvoice
    hibtools 'org.hibernate:hibernate-tools:4.+',
        'mysql:mysql-connector-java:5.+'
    hibtools files("$buildDir/classes/main") // MyBook and MyBooking
}

task createSchema(dependsOn: ['build', 'copyPersistenceUnit']) << {     
    ant.taskdef(name: 'hibernatetool',
            classname: 'org.hibernate.tool.ant.HibernateToolTask',
            classpath: configurations.hibtools.asPath)
    ant.hibernatetool(destdir: 'schema') {
        ant.jpaconfiguration(persistenceunit: 'defaultPersistenceUnit')
        ant.hbm2ddl(drop: 'false', export: 'false', outputfilename: 'mydb.sql')
    }
}

task copyPersistenceUnit(type: Copy) {
    from "$buildDir/resources/main/META-INF/persistence.xml"
    into "$buildDir/classes/main/META-INF/"
}

结果是一个包含MyCustomer、MyInvoice、MyBook、MyBooking表的动态链接库,即使MyBook和MyBooking没有列出,并且可以在不接触配置的情况下添加或删除。

这里的诀窍是将persistence.xml文件从资源文件夹复制到classes文件夹。如果您不这样做,为了找到它,您需要将资源路径添加到hibtools配置中,如下所示:

代码语言:javascript
复制
hibtools files(["$buildDir/resources/main", "$buildDir/classes/main"])

但是这样做将防止您的应用程序实体被发现。

这适用于Hibernate 4。使用当前的Hibernate 5 alpha工具会得到一个空的ddl文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36228135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档