清除整个数据库(用于使用Hibernate进行单元测试)

编程入门 行业动态 更新时间:2024-10-26 00:31:06
本文介绍了清除整个数据库(用于使用Hibernate进行单元测试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的单元测试使用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进行单元测试)

本文发布于:2023-10-24 08:30:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523450.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   数据库   Hibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!