配置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的最低配置相关推荐
- 测试Hibernate的最低配置
介绍 在上一篇文章中,我宣布了我打算创建个人Hibernate课程的意图. 首先要做的是最小的测试配置. 这些示例与Hibernate 4有关. 您只需要休眠 在实际的生产环境中,您不会单独使用Hib ...
- 使命召唤 计算机配置,使命召唤17电脑配置要求高吗 COD17最低配置介绍_游侠网...
使命召唤17电脑配置要求高吗?近日官方公布了游戏的PC配置要求,小编为大家进行了简单的整理,一起来看下COD17最低配置介绍吧. COD17最低配置介绍 最低配置要求: 操作系统:Windows 7 ...
- 计算机低配配置单,吃鸡需要什么配置|电脑玩绝地求生最低配置多少
吃鸡游戏要求什么样的配置?pubg绝地求生现在特别火,对电脑硬件要求特别高,内存就需要6G或8G,很多玩家忍痛剁手,因为现在内存价格飙升,那么绝地求生大逃杀要求怎样的硬件配置呢?下面小编跟大家介绍一下 ...
- cuisineroyale服务器所在位置,cuisine royale配置要求是什么?最低配置要求介绍
cuisine royale配置要求是什么?最近很多的玩家都很喜欢这款恶搞的吃鸡游戏,那么这款游戏的最低配置是什么呢?下面小编就来为大家详细的介绍一下cuisine royale最低配置要求介绍,感兴 ...
- cad画图要求计算机最低配置,2018CAD对笔记本电脑的最低配置要求?
需要达到这个配置及以上: 主板: GA-MA78GM-S2H CPU: AMD x4 940 内存: Kingston DDR2 800 2GB*4 显卡: 主板集成HD4200 硬盘: WD10EA ...
- linux和win10哪个要求配置高,win10配置要求高吗?Win10的最低配置要求是什么?
用上成熟稳定的系统,相信win10会是你的首选,在win10的身上,我们可以看到微软在不断提升用户的体验,同时也完善了win8遗留下来的问题,但一个好的系统,少不了硬件上的支持.下面就让我们来看看wi ...
- 官方配置要求_赛博朋克2077的推荐配置和最低配置
赛博朋克2077官方配置要求 赛博朋克2077官方配置要求 不久前赛博朋克在其官方网站发布了最新的电脑配置要求(台式机),比以往大家所猜测的配置要求要来得低一些,下面这份赛博朋克2077官方电脑配置要 ...
- Hibernate实体映射配置1(java@注解方式)
实体和数据库之间存在某种映射关系,hibernate根据这种映射关系完成数据的存取.在程序中这种映射关系由映射文件(*.hbm.xml)或者java注解(@)定义. 本文以java注解的形式总结映射关 ...
- windows7是计算机硬件吗,Windows7硬件最低配置要求介绍
微软宣布2014年4月8日即将停止对xp的技术支持,于是出于电脑安全的考虑,很多xp系统的用户选择了将操作系统升级到win7.win7作为新一代的操作系统,和xp系统相比有了很多的不同,出了安全性能更 ...
最新文章
- 百度拿不到Big Data资源,7h删抓紧时间!!
- mysql.server 文件是什么_mysql的启动脚本mysql.server及示例配置文件
- 学习笔记 --- 编码过程中常见的三种异步方式
- 如何设计让用户喜欢且令人尖叫的产品?
- vuex登录后设置token
- linux之权限管理_1
- 【整理】fiddler不能监听 localhost和 127.0.0.1的问题
- 【linux】——linux ls命令参数及用法详解---linux显示目录内容命令
- [Codeforces673A]Bear and Game(水题,思路)
- android4.2 音频模块启动分析,Android 音频模块学习小结
- 黑马程序员全套Java教程_Java基础教程_目录
- uni实现前端分页功能
- 自己动手开发网络服务器(一)
- 快速使用 Docker 部署 Spring Boot 项目
- 通过京东技术演进和淘宝技术演进,探察未来技术和架构
- 怎么给当前计算机添加一个用户,电脑如何设置新用户 电脑创建新用户的方法...
- Defcon China 靶场题 - 内网渗透Writeup
- 稀里糊涂地被评为博客之星的候选人了,那就麻烦大家帮忙投个票吧~
- AI医学诊断基础-CT扫描、核磁共振成像(MRI)、拍X光、拍胸片、做B超/彩超等常规检查的介绍、原理、医学影像示例(持续跟新和答疑。。。)
- python中的命令,python学习——python中命令行
热门文章
- ssl2346-联络员【图论,最小生成树】
- codeforces1452 E. Two Editorials
- 非阻塞线程安全列表——ConcurrentLinkedDeque应用举例
- mysql语句性能开销检测profiling详解
- @Resource,@Autowired,@Inject3种注入方式详解
- ssh(Spring+Spring mvc+hibernate)——DeptDaoImpl.java
- java中判断 101-200 之间有多少个素数,并输出所有的素数
- 要么干,要么滚,千万别混
- 复制中含有非法字符导致的错误
- Android中SlidingDrawer开发报错You need to use a Theme.AppCompat theme (or descendant) with this activity.