maven jpa_使用Hibernate 4,JPA和Maven的架构创建脚本
maven jpa
這種情況很簡(jiǎn)單–您想要在構(gòu)建應(yīng)用程序時(shí)生成數(shù)據(jù)庫(kù)模式創(chuàng)建腳本(然后在目標(biāo)數(shù)據(jù)庫(kù)上執(zhí)行腳本),這在Hibernate 3中相對(duì)容易,因?yàn)橛?hibernate3-maven-plugin ,但是與Hibernate 4不兼容。當(dāng)然,對(duì)于每個(gè)新項(xiàng)目,都應(yīng)從Hibernate 4開(kāi)始。 那么該怎么辦? 它相對(duì)簡(jiǎn)單,但是需要花費(fèi)一些時(shí)間進(jìn)行研究和測(cè)試。 這個(gè)想法是使用SchemaExport工具。 但這有點(diǎn)棘手,因?yàn)樗鼉H支持本地Hibernate配置,而不支持JPA。首先,創(chuàng)建一個(gè)處理導(dǎo)出的命令行應(yīng)用程序。 請(qǐng)注意,不建議使用Ejb3Configuration,但不建議將其用于外部使用-Hibernate在內(nèi)部大量使用了它。 因此,這是一個(gè)正常的工作類(lèi):
@SuppressWarnings('deprecation') public class JpaSchemaExport {public static void main(String[] args) throws IOException {execute(args[0], args[1], Boolean.parseBoolean(args[2]), Boolean.parseBoolean(args[3]));}public static void execute(String persistenceUnitName, String destination, boolean create, boolean format) {System.out.println('Starting schema export');Ejb3Configuration cfg = new Ejb3Configuration().configure(persistenceUnitName, new Properties());Configuration hbmcfg = cfg.getHibernateConfiguration();SchemaExport schemaExport = new SchemaExport(hbmcfg);schemaExport.setOutputFile(destination);schemaExport.setFormat(format);schemaExport.execute(true, false, false, create);System.out.println('Schema exported to ' + destination);} }請(qǐng)注意,我們沒(méi)有將文件直接部署到目標(biāo)數(shù)據(jù)庫(kù)。 (.execute的第二個(gè)參數(shù)為false)。 這是因?yàn)樵趐ersistence.xml中沒(méi)有數(shù)據(jù)庫(kù)連接屬性-它們是外部的。 稍后在maven構(gòu)建中完成架構(gòu)文件的部署,但這超出了本文的范圍。
然后,我們只需要從Maven構(gòu)建中調(diào)用此類(lèi)。 我最初嘗試將其創(chuàng)建為ant任務(wù),并使用antrun插件運(yùn)行它,但是它存在類(lèi)路徑和類(lèi)加載器問(wèn)題(找不到實(shí)體和persistence.xml)。 這就是為什么我使用exec-maven-plugin的原因,該插件在運(yùn)行構(gòu)建的同一JVM中調(diào)用該應(yīng)用程序:
<plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.1</version><executions><execution><phase>${sql.generation.phase}</phase> <!-- this is process-classes in our case currently --><goals><goal>java</goal></goals></execution></executions><configuration><mainClass>com.yourcompany.util.JpaSchemaExport</mainClass><arguments><argument>core</argument><argument>${project.build.directory}/classes/schema.sql</argument><argument>true</argument><argument>true</argument></arguments></configuration> </plugin>然后,您可以使用sql-maven-plugin將schema.sql文件部署到目標(biāo)數(shù)據(jù)庫(kù)(您將需要由maven加載外部化的db屬性,這由properties-maven-plugin完成)。
參考: 如何使用我們的JCG合作伙伴 Bozhidar Bozhanov (來(lái)自Bozho的技術(shù)博客)上的Hibernate 4,JPA和Maven生成模式創(chuàng)建腳本 。
翻譯自: https://www.javacodegeeks.com/2012/07/schema-creation-script-with-hibernate-4.html
maven jpa
總結(jié)
以上是生活随笔為你收集整理的maven jpa_使用Hibernate 4,JPA和Maven的架构创建脚本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 服务器linux系统安装(服务器 lin
- 下一篇: 跨站点脚本(xss)_跨站点脚本(XSS