配置hibernate_测试Hibernate的最低配置
配置hibernate
介紹
在上一篇文章中,我宣布了我打算創(chuàng)建個(gè)人Hibernate課程的意圖。 首先要做的是最小的測(cè)試配置。 這些示例與Hibernate 4有關(guān)。
您只需要Hibernate
在實(shí)際的生產(chǎn)環(huán)境中,您不會(huì)單獨(dú)使用Hibernate,因?yàn)槟梢詫⑵浼傻絁EE或Spring容器中。 為了測(cè)試Hibernate功能,您不需要完整的框架堆棧,您只需依賴Hibernate靈活的配置選項(xiàng)即可。
情況1:基于驅(qū)動(dòng)程序的JDBC配置
我們首先定義一個(gè)測(cè)試實(shí)體:
@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抽象層,我們不必強(qiáng)迫使用任何外部事務(wù)管理器,也不必編寫任何自制的事務(wù)管理代碼。
為了進(jìn)行測(cè)試,我們可以使用JDBC資源本地事務(wù),該事務(wù)由默認(rèn)的JdbcTransactionFactory內(nèi)部管理。
我們甚至不需要提供外部數(shù)據(jù)源,因?yàn)镠ibernate提供了一個(gè)由DriverManagerConnectionProviderImpl表示的非生產(chǎn)內(nèi)置連接池。
我們的測(cè)試代碼如下所示:
@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();}} }我們不需要任何外部配置文件,因此這是我們可以構(gòu)建和配置會(huì)話工廠的方式:
@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:使用專業(yè)的連接池
如果我們想用專業(yè)的連接池代替內(nèi)置的連接池,Hibernate提供了設(shè)置c3p0的選擇,該設(shè)置由C3P0ConnectionProvider在內(nèi)部處理。
我們只需要更改會(huì)話工廠配置屬性:
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:使用外部數(shù)據(jù)源
由于Hibernate不會(huì)記錄SQL預(yù)準(zhǔn)備語(yǔ)句參數(shù):
o.h.SQL - insert into SecurityId (id, role) values (default, ?)我們將添加一個(gè)datasource-proxy來(lái)攔截實(shí)際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; }結(jié)論
這是測(cè)試Hibernate功能所需的最低配置設(shè)置。 每當(dāng)我提交帶有復(fù)制測(cè)試用例的Hibernate錯(cuò)誤報(bào)告時(shí),我也會(huì)使用這些配置。
- 代碼可在GitHub上獲得 。
翻譯自: https://www.javacodegeeks.com/2014/06/the-minimal-configuration-for-testing-hibernate.html
配置hibernate
總結(jié)
以上是生活随笔為你收集整理的配置hibernate_测试Hibernate的最低配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 家访目的写什么 家访什么目的
- 下一篇: 人偶怎么做 如何自己做人偶玩具