欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

配置hibernate_测试Hibernate的最低配置

发布时间:2023/12/3 56 豆豆
生活随笔 收集整理的这篇文章主要介绍了 配置hibernate_测试Hibernate的最低配置 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

配置hibernate

介绍

在上一篇文章中,我宣布了我打算创建个人Hibernate课程的意图。 首先要做的是最小的测试配置。 这些示例与Hibernate 4有关。

您只需要Hibernate

在实际的生产环境中,您不会单独使用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

总结

以上是生活随笔为你收集整理的配置hibernate_测试Hibernate的最低配置的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。