我的单元测试使用Hibernate连接到内存中的HSQLDB数据库.我希望可以使用JUnit的TestCase.setUp()方法清除并重新创建数据库(包括模式和所有数据的整个数据库).
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.
推荐答案您可以配置休眠配置文件以强制数据库每次重新创建表和架构.
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创建SessionFactory时,自动将模式DDL验证或导出到数据库.使用create-drop可以在显式关闭SessionFactory时删除数据库架构.
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.
例如验证更新|创建|创建拖放
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.
更多推荐
清除整个数据库(用于使用Hibernate进行单元测试)
发布评论