java 清除整个数据库(用于使用 Hibernate 进行单元测试)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3646661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clearing Entire Database (for unit testing with Hibernate)
提问by Hyman Edmonds
My unit tests use Hibernate to connect to an in-memory HSQLDB database. I was hoping there would be a way to clear and recreate the database (the entire database including the schema and all the data) in JUnit's TestCase.setUp()
method.
我的单元测试使用 Hibernate 连接到内存中的 HSQLDB 数据库。我希望有一种方法可以在 JUnit 的TestCase.setUp()
方法中清除和重新创建数据库(整个数据库,包括模式和所有数据)。
采纳答案by mhshams
you can config your hibernate configuration file to force database to recreate your tables and schema every time.
您可以配置您的休眠配置文件以强制数据库每次重新创建您的表和架构。
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create-drop</property>
hibernate.hbm2ddl.auto Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.
hibernate.hbm2ddl.auto 在创建 SessionFactory 时自动验证或导出模式 DDL 到数据库。使用 create-drop,当 SessionFactory 显式关闭时,数据库模式将被删除。
e.g. validate | update | create | create-drop
例如验证| 更新 | 创建 | 创建删除
if you don't like to have this config in your real hibernate config, you can create one hibernate config for unit testing purpose.
如果您不喜欢在真正的休眠配置中使用此配置,您可以创建一个休眠配置用于单元测试目的。
回答by rhinds
If you are using Spring, then you can use the @Transactional attribute on your unit test, and by default at the end of every unit test all persisted data will be automatically rolled back so you dont need to worry about dropping the tables every time.
如果您使用的是 Spring,那么您可以在单元测试中使用 @Transactional 属性,默认情况下,在每个单元测试结束时,所有持久化数据都将自动回滚,因此您无需担心每次都删除表。
I haa walked throug an example here http://automateddeveloper.blogspot.com/2011/05/hibernate-spring-testing-dao-layer-with.html
我在这里做了一个例子http://automateddeveloper.blogspot.com/2011/05/hibernate-spring-testing-dao-layer-with.html
回答by Bozho
hibernate.hbm2ddl.auto=create-drop
And bootstrap a new SessionFactory
.
并引导一个新的SessionFactory
.
回答by Sairam Krish
From testing perspective, the best practice is to clear data after every single test. If you use create-drop, it will also drop the table schema. This causes an overhead of recreating the schema everytime.
从测试的角度来看,最佳实践是在每次测试后清除数据。如果您使用 create-drop,它也会删除表架构。这会导致每次重新创建模式的开销。
Since you are using hsql, which provides a direct mechanism to truncate, it would be the best option in this case.
由于您使用的是 hsql,它提供了一种直接的截断机制,因此在这种情况下它将是最佳选择。
@After
public void clearDataFromDatabase() {
//Start transaction, based on your transaction manager
dao.executeNativeQuery("TRUNCATE SCHEMA PUBLIC AND COMMIT");
//Commit transaction
}
回答by Brad Lee
Be careful with wiping the world and starting over fresh each time. Soon, you will likely want to start with a "default" set of test data loaded in your system. Thus, what you really want is to revert to that base state before each test is ran. In this case, you want a Transaction which rollsbackbefore each test run.
小心擦拭世界并每次都重新开始。很快,您可能希望从系统中加载的“默认”测试数据集开始。因此,您真正想要的是在运行每个测试之前恢复到该基本状态。在这种情况下,您需要一个在每次测试运行之前回滚的事务。
To accomplish this, you should annotate your JUnit class:
要做到这一点,您应该注释您的 JUnit 类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/path/to/spring-config.xml"})
@TransactionConfiguration(transactionManager="myTransactionManager", defaultRollback=true)
public class MyUnitTestClass {
...
}
And then annotate each of your test methods with @Transactional:
然后使用@Transactional 注释每个测试方法:
@Transactional
@Test
public void myTest() {
...
}