maven jpa_使用Hibernate 4,JPA和Maven的架构创建脚本
maven jpa
這種情況很簡單–您想要在構建應用程序時生成數據庫模式創建腳本(然后在目標數據庫上執行腳本),這在Hibernate 3中相對容易,因為有 hibernate3-maven-plugin ,但是與Hibernate 4不兼容。當然,對于每個新項目,都應從Hibernate 4開始。 那么該怎么辦? 它相對簡單,但是需要花費一些時間進行研究和測試。 這個想法是使用SchemaExport工具。 但這有點棘手,因為它僅支持本地Hibernate配置,而不支持JPA。首先,創建一個處理導出的命令行應用程序。 請注意,不建議使用Ejb3Configuration,但不建議將其用于外部使用-Hibernate在內部大量使用了它。 因此,這是一個正常的工作類:
@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);} }請注意,我們沒有將文件直接部署到目標數據庫。 (.execute的第二個參數為false)。 這是因為在persistence.xml中沒有數據庫連接屬性-它們是外部的。 稍后在maven構建中完成架構文件的部署,但這超出了本文的范圍。
然后,我們只需要從Maven構建中調用此類。 我最初嘗試將其創建為ant任務,并使用antrun插件運行它,但是它存在類路徑和類加載器問題(找不到實體和persistence.xml)。 這就是為什么我使用exec-maven-plugin的原因,該插件在運行構建的同一JVM中調用該應用程序:
<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文件部署到目標數據庫(您將需要由maven加載外部化的db屬性,這由properties-maven-plugin完成)。
參考: 如何使用我們的JCG合作伙伴 Bozhidar Bozhanov (來自Bozho的技術博客)上的Hibernate 4,JPA和Maven生成模式創建腳本 。
翻譯自: https://www.javacodegeeks.com/2012/07/schema-creation-script-with-hibernate-4.html
maven jpa
總結
以上是生活随笔為你收集整理的maven jpa_使用Hibernate 4,JPA和Maven的架构创建脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器linux系统安装(服务器 lin
- 下一篇: 跨站点脚本(xss)_跨站点脚本(XSS