日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ddl hibernate_Hibernate:DDL模式生成

發布時間:2023/12/3 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ddl hibernate_Hibernate:DDL模式生成 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ddl hibernate

不久前,我必須使用內存數據庫。 該活動與集成測試有關。 如您所知,通常將內存數據庫用于集成測試。 造成這種情況的原因有很多:可移植性,良好的環境基礎結構,高性能,原始數據庫的一致性。

問題在于將生產DDL架構壓入測試內存數據庫。 第一個是MySQL ,第二個是HSQLDB 。 MySQL的語法不同于HSQL語法。 因此,如果不進行適當的轉換,就不可能將MySQL表模式導入HSQLDB。

坦白說,我已經花了很多時間在尋找一些解決方案,這將有助于我在HSQL中導入MySQL DDL模式 。 結果不是我想要的那么好。 所有解決方案都是商業化的或非自動化的,例如,替換HSQL上所有MySQL特定的代碼。

幸運的是,我的項目使用Hibernate作為JPA實現。 所有實體都裝飾有適當的Hibernate注釋。 正如您將進一步看到的那樣,這對于MySQL模式的轉換將非常有幫助。 Hibernate提供了基于DDL生成實體的機制,反之亦然。 因此,任何使用Hibernate注釋修飾的實體都可以使用Hibernate支持的DB語言來表示為表模式。

這是一個解決我的問題的類:

public class SchemaTranslator {private Configuration config = null;public SchemaTranslator() {config = new Configuration();}public SchemaTranslator setDialect(String dialect) {config.setProperty(AvailableSettings.DIALECT, dialect);return this;}/*** Method determines classes which will be used for DDL generation. * @param annotatedClasses - entities annotated with Hibernate annotations.*/public SchemaTranslator addAnnotatedClasses(Class[] annotatedClasses) {for (Class clazz : annotatedClasses)config.addAnnotatedClass(clazz);return this;}/*** Method performs translation of entities in table schemas.* It generates 'CREATE' and 'DELETE' scripts for the Hibernate entities.* Current implementation involves usage of {@link #write(FileOutputStream, String[], Formatter)} method.* @param outputStream - stream will be used for *.sql file creation.* @throws IOException*/public SchemaTranslator translate(FileOutputStream outputStream) throws IOException {Dialect requiredDialect = Dialect.getDialect(config.getProperties());String[] query = null;query = config.generateDropSchemaScript(requiredDialect);write(outputStream, query, FormatStyle.DDL.getFormatter());query = config.generateSchemaCreationScript(requiredDialect);write(outputStream, query, FormatStyle.DDL.getFormatter());return this;}/*** Method writes line by line DDL scripts in the output stream.* Also each line logs in the console.* @throws IOException*/private void write(FileOutputStream outputStream, String[] lines, Formatter formatter) throws IOException {String tempStr = null;for (String line : lines) {tempStr = formatter.format(line)+";";System.out.println(tempStr);outputStream.write(tempStr.getBytes());}}public static void main(String[] args) throws IOException {SchemaTranslator translator = new SchemaTranslator();Class[] entityClasses = {Smartphone.class};translator.setDialect("org.hibernate.dialect.HSQLDialect").addAnnotatedClasses(entityClasses).translate(new FileOutputStream(new File("db-schema.sql")));}}

上面的代碼非常冗長,但我對此進行了評論。 因此,我希望它或多或少容易理解。 整個應用程序的代碼可以在GitHub上找到 。 SchemaTranslator類在項目結構中具有以下位置:

  • / src / test / java / com / mobapp / test / util /

借助此類,您可以將您的實體采用到Hibernate支持的任何必需數據庫中。

祝好運!

參考: Hibernate:我們的JCG合作伙伴 Alexey Zvolinskiy在Fruzenshtein的注釋博客中生成了DDL Schema 。

翻譯自: https://www.javacodegeeks.com/2014/01/hibernate-ddl-schema-generation.html

ddl hibernate

總結

以上是生活随笔為你收集整理的ddl hibernate_Hibernate:DDL模式生成的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。