调用就是hbm2ddl是否有gradle这个插件或调用休眠的工具就是hbm2ddl任务生成从注解的类数据库架构的任何其他方式,而不必列出所有的实体(@Entity)在一些配置文件,但有他们在类路径中发现?从gradle这个
优选地,用于休眠5,但休眠4也都行。
调用就是hbm2ddl是否有gradle这个插件或调用休眠的工具就是hbm2ddl任务生成从注解的类数据库架构的任何其他方式,而不必列出所有的实体(@Entity)在一些配置文件,但有他们在类路径中发现?从gradle这个
优选地,用于休眠5,但休眠4也都行。
我终于做到了通过移动persistence.xml文件发生。
在我的情况,我有一些图书馆的实体和我想要生成模式应用实体。显然,我需要在persistence.xml中列出库实体,这很好,因为它们不会经常更改,但为了让应用程序实体从类路径中选出而不在持久性文件中列出它们,我必须确保类和persistence.xml文件都由相同的类加载器加载(我猜)。
这是行之有效的。
库实体:MyCustomer,MyInvoice
应用实体:MyBook,myBooking的
/src/main/resources/META-INF/persistence.xml
<?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:
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,My表的ddl Book,MyBooking,即使MyBook和MyBooking没有在任何地方列出,也可以在不触摸配置的情况下添加或删除。
这里的窍门是persistence.xml文件从资源文件夹复制到classes文件夹。 如果你不这样做,才能拥有它找到你需要的资源路径的东西,如添加到hibtools配置:
hibtools files(["$buildDir/resources/main", "$buildDir/classes/main"])
,但这样做会阻止你的应用实体不被发现。
这工作与Hibernate 4.使用休眠电流5阿尔法工具给出了一个空的DDL文件。