测试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的最低配置相关推荐
- 配置hibernate_测试Hibernate的最低配置
配置hibernate 介绍 在上一篇文章中,我宣布了我打算创建个人Hibernate课程的意图. 首先要做的是最小的测试配置. 这些示例与Hibernate 4有关. 您只需要Hibernate 在 ...
- Windows 11 首个预览版发布,最低配置要求或降低!
整理 | 郑丽媛 出品 | CSDN(ID:CSDNnews) 自上周五 Windows 11 官宣,就有不少人在期待着年底正式版的到来.而在今天,有一部分人就可以提前体验 Windows 11 了: ...
- Hibernate连接池配置实例
Hibernate支持第三方的连接池,官方推荐的连接池是C3P0,Proxool,以及DBCP.在Hibernate连接池配置时需要注意的有三点: 一.Apche的DBCP在Hibernate2中受支 ...
- hibernate教程--常用配置和核心API详解
一.Hibernate的常用的配置及核心API. 1.1 Hibernate的常见配置: 1.1.1.核心配置: 核心配置有两种方式进行配置: 1)属性文件的配置: * hibernate.prop ...
- hibernate.hbm2ddl.auto配置详解
hibernate.hbm2ddl.auto配置详解 http://www.cnblogs.com/feilong3540717/archive/2011/12/19/2293038.html hib ...
- Hibernate的基本配置
1.导入 jar包 2 .新建实体类 3.把实体类转化成xml文件 3.1 在 xml 进行配置 <?xml version="1.0" encoding="UTF ...
- hibernate.hbm2ddl.auto配置及意义
*********************************************************** 这两天在整理Spring + JPA(Hibernate实现),从网上copy了 ...
- 王牌战争服务器维护中多少才能玩,王牌战争最低配置要求一览 什么手机可以玩...
王牌战争什么手机可以玩?最低配置要求是什么?游戏中是对我们手机的配置有要求的,很多玩家都玩不了,下面由第一手游网小编为大家带来最低配置要求一览吧! 王牌战争最低配置要求一览 王牌战争不删档测试支持机型 ...
- revit 对计算机最低配置,了解revit对电脑配置要求是什么
一些网友想知道:在电脑上安装revit这一款软件的话, Revit对电脑配置的要求.在今天的教程中,我们就给大家整理一下Revit电脑配置要求的详细介绍,方便大家选择合适的电脑进行安装,一般来说,Re ...
最新文章
- SolidWorks大师班:从基础到专业学习教程
- PNAS:多年多点5千样本鉴定玉米根际可遗传微生物
- 【转】Linux面试题集锦
- 自动工作负载信息库 AWR
- 陈大惠老师:什么叫道德?
- Asp.Net SignalR - 简单聊天室实现
- 猎洞高手轻松变身Gsuite 超级管理员接管他人的 Gsuite 账户
- 生产者-消费者习题的运用
- python 中的socket_python中的socket概述
- CISCO技术(1.7万)
- (struct)结构体变量作为函数参数调用的方法小结
- 惠普打印机,打印状态:需要注意,打印时显示:需要用户干
- iOS UI第一阶段笔记
- Python values()函数用法
- windows10网络共享及重启后失效的解决办法
- Unity SteamVR报错问题却影响运行的记录(Log path could not be located (112)“)
- 地图和地理空间革命:地理学大规模开放在线课堂(MOOC)
- 【探花交友DAY 08】左滑不喜欢右滑喜欢以及附近的人
- 关于运维的标准(ITSS信息技术服务-运行维护)介绍
- wps怎么把字缩到最小_wps文字怎么把空行缩小
热门文章
- EasyExcel中输出为时间格式
- 2018蓝桥杯省赛---java---B---2(方格计数)
- Android 对话框,确定取消
- 配置struts.xml时extends=struts-default会报错,原因和解决
- 如何彻底删除MySQL数据库(保姆级教学)
- php面试心得,php面试题的总结
- java迭代器删除两个_两个迭代器的故事
- neo4j 连接超时_Neo4j:遍历查询超时
- 五皇后问题 java_Java的5个古怪问题
- jar 、war、ear_在命令行上操作JAR,WAR和EAR