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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

测试Hibernate的最低配置

發布時間:2023/12/3 编程问答 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 测试Hibernate的最低配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

介紹

在上一篇文章中,我宣布了我打算創建個人Hibernate課程的意圖。 首先要做的是最小的測試配置。 這些示例與Hibernate 4有關。

您只需要休眠

在實際的生產環境中,您不會單獨使用Hibernate,因為您可以將其集成到JEE或Spring容器中。 要測試Hibernate功能,您不需要完整的框架堆棧,只需依賴Hibernate靈活的配置選項即可。

情況1:基于驅動程序的JDBC配置

我們首先定義一個測試實體:

@Entity class SecurityId {@Id@GeneratedValueprivate Long id;private String role;public Long getId() {return id;}public String getRole() {return role;}public void setRole(String role) {this.role = role;} }

多虧了Hibernate Transaction抽象層,我們不必強迫使用任何外部事務管理器,也不必編寫任何自制的事務管理代碼。

為了進行測試,我們可以使用JDBC資源本地事務,該事務由默認的JdbcTransactionFactory內部管理。

我們甚至不需要提供外部數據源,因為Hibernate提供了一個由DriverManagerConnectionProviderImpl表示的非生產內置連接池。

我們的測試代碼如下:

@Test public void test() {Session session = null;Transaction txn = null;try {session = sf.openSession();txn = session.beginTransaction();SecurityId securityId = new SecurityId();securityId.setRole("Role");session.persist(securityId);txn.commit();} catch (RuntimeException e) {if ( txn != null && txn.isActive() ) txn.rollback();throw e;} finally {if (session != null) {session.close();}} }

我們不需要任何外部配置文件,因此這是我們可以構建和配置會話工廠的方式:

@Override protected SessionFactory newSessionFactory() {Properties properties = new Properties();properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");//log settingsproperties.put("hibernate.hbm2ddl.auto", "update");properties.put("hibernate.show_sql", "true");//driver settingsproperties.put("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:test");properties.put("hibernate.connection.username", "sa");properties.put("hibernate.connection.password", "");return new Configuration().addProperties(properties).addAnnotatedClass(SecurityId.class).buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(properties).build()); }

情況2:使用專業的連接池

如果我們想用專業的連接池來代替內置的連接池,Hibernate提供了設置c3p0的選擇,該設置由C3P0ConnectionProvider在內部處理。

我們只需要更改會話工廠配置屬性:

protected SessionFactory newSessionFactory() {Properties properties = new Properties();properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");//log settingsproperties.put("hibernate.hbm2ddl.auto", "update");properties.put("hibernate.show_sql", "true");//driver settingsproperties.put("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:test");properties.put("hibernate.connection.username", "sa");properties.put("hibernate.connection.password", "");//c3p0 settingsproperties.put("hibernate.c3p0.min_size", 1);properties.put("hibernate.c3p0.max_size", 5);return new Configuration().addProperties(properties).addAnnotatedClass(SecurityId.class).buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(properties).build()); }

情況3:使用外部數據源

由于Hibernate不會記錄SQL預準備語句參數:

o.h.SQL - insert into SecurityId (id, role) values (default, ?)

我們將添加一個datasource-proxy來攔截實際的SQL查詢:

n.t.d.l.SLF4JQueryLoggingListener - Name: Time:0 Num:1 Query:{[insert into SecurityId (id, role) values (default, ?)][Role]}

配置如下所示:

@Override protected SessionFactory newSessionFactory() {Properties properties = new Properties();properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");//log settingsproperties.put("hibernate.hbm2ddl.auto", "update");//data source settingsproperties.put("hibernate.connection.datasource", newDataSource());return new Configuration().addProperties(properties).addAnnotatedClass(SecurityId.class).buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(properties).build()); }private ProxyDataSource newDataSource() {JDBCDataSource actualDataSource = new JDBCDataSource();actualDataSource.setUrl("jdbc:hsqldb:mem:test");actualDataSource.setUser("sa");actualDataSource.setPassword("");ProxyDataSource proxyDataSource = new ProxyDataSource();proxyDataSource.setDataSource(actualDataSource);proxyDataSource.setListener(new SLF4JQueryLoggingListener());return proxyDataSource; }

結論

這是測試Hibernate功能所需的最低配置設置。 每當我提交帶有復制測試用例的Hibernate錯誤報告時,我也會使用這些配置。

  • 代碼可在GitHub上獲得 。

翻譯自: https://www.javacodegeeks.com/2014/06/the-minimal-configuration-for-testing-hibernate.html

總結

以上是生活随笔為你收集整理的测试Hibernate的最低配置的全部內容,希望文章能夠幫你解決所遇到的問題。

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