調用就是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文件。