Spring / Hibernate测试:在创建DDL后插入测试数据 [英] Spring/Hibernate testing: Inserting test data after DDL creation

查看:130
本文介绍了Spring / Hibernate测试:在创建DDL后插入测试数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring / Hibernate Web应用程序,它有一些在内存中的HSQL数据库上运行的集成测试。由于hbm2ddl = create,Hibernate使用这个空白数据库并创建所有测试表和约束。但是,我有一个新的bean,它在afterPropertiesSet()方法期间从数据库中检查特定的配置值,所以当这个bean被初始化时,这样的行需要存在于数据库中。



有没有什么好方法可以设置Rail的测试装置的Java / Spring / Hibernate等价物?我试图找到一种方法来告诉Hibernate每当你创建这个表时,立即插入这些行。

解决方案


我试图找到一种方法来告诉Hibernate每当你创建这个表时,立即插入这些行


从Hibernate 3.1中,您可以在Hibernate的运行时类路径中包含名为 import.sql 的文件,并且在模式导出时,Hibernate将执行包含在该文件中的SQL语句该模式已导出。

此功能已在鹿特丹JBug和Hibernate的导入.sql 博客文章:


import.sql:轻松地在单元测试中导入数据



Hibernate有一个整洁的小功能
,它是大量记录不足和
未知。您可以在数据库模式
生成后立即在 SessionFactory 创建
期间执行SQL脚本
,以将数据导入新鲜
数据库。您只需要在您的类路径
root中添加一个名为 import.sql 的文件
,并设置 create
create-drop 作为您的
hibernate.hbm2ddl.auto 属性。

我将它用于
中的Hibernate Search现在我开始执行
查询章节。它使用
我的单元测试的一组新数据初始化我的
数据库。在不同的例子中,JBoss Seam也使用它

import.sql 是一个非常简单的功能
,但在时间上非常有用。记住
,SQL可能依赖
数据库(可移植性!)。

$ $ p $ $ $ $ $ $#$ .sql文件
从产品
中删除插入到产品(PROD_ID,ASIN,TITLE,PRICE,IMAGE_URL,DESCRIPTION)values('1','630522577X','My Fair Lady',19.98,'630522577X .jpg','我的公平等等等等');
插入到产品(PROD_ID,ASIN,TITLE,PRICE,IMAGE_URL,DESCRIPTION)值('2','B00003CXCD','罗马假日',12.98,'B00003CXCD.jpg','我们可以争辩说,等等等等);

有关此
功能的更多信息,请查看 Eyal的博客,他
写了一个很好的小条目。
请记住,如果您想添加额外的
数据库对象(索引,表和
等),还可以使用辅助
数据库对象功能。


它还没有真正记录。


I have a Spring/Hibernate webapp that has some integration tests that run on an in-memory HSQL database. Hibernate takes this blank database and creates all of my test tables and constraints thanks to hbm2ddl=create. However, I have a new bean that checks for a particular config value from the database during its afterPropertiesSet() method, and so when this bean is initialized, such a row needs to exist in the database.

Is there any good way to set up a Java/Spring/Hibernate equivalent of Rail's test fixtures? I'm trying to find a way to tell Hibernate "whenever you create this table, insert these rows immediately afterwards". I couldn't find a callback or a hook I could add, but maybe there's another way.

解决方案

I'm trying to find a way to tell Hibernate "whenever you create this table, insert these rows immediately afterwards"

Since Hibernate 3.1, you can include a file called import.sql in the runtime classpath of Hibernate and at the time of schema export, Hibernate will execute the SQL statements contained in that file after the schema has been exported.

This feature has been announced in the Rotterdam JBug and Hibernate's import.sql blog post:

import.sql: easily import data in your unit tests

Hibernate has a neat little feature that is heavily under-documented and unknown. You can execute an SQL script during the SessionFactory creation right after the database schema generation to import data in a fresh database. You just need to add a file named import.sql in your classpath root and set either create or create-drop as your hibernate.hbm2ddl.auto property.

I use it for Hibernate Search in Action now that I have started the query chapter. It initializes my database with a fresh set of data for my unit tests. JBoss Seam also uses it a lot in the various examples. import.sql is a very simple feature but is quite useful at time. Remember that the SQL might be dependent on your database (ah portability!).

#import.sql file
delete from PRODUCTS
insert into PRODUCTS (PROD_ID, ASIN, TITLE, PRICE, IMAGE_URL, DESCRIPTION) values ('1', '630522577X', 'My Fair Lady', 19.98, '630522577X.jpg', 'My Fair blah blah...');
insert into PRODUCTS (PROD_ID, ASIN, TITLE, PRICE, IMAGE_URL, DESCRIPTION) values ('2', 'B00003CXCD', 'Roman Holiday ', 12.98, 'B00003CXCD.jpg', 'We could argue that blah blah');

For more information about this feature, check Eyal's blog, he wrote a nice little entry about it. Remember if you want to add additional database objects (indexes, tables and so on), you can also use the auxiliary database objects feature.

It is still not really documented.

这篇关于Spring / Hibernate测试:在创建DDL后插入测试数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆