Java Spring/Hibernate 测试:在 DDL 创建后插入测试数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3408991/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:04:00  来源:igfitidea点击:

Spring/Hibernate testing: Inserting test data after DDL creation

javaunit-testinghibernatespringtest-data

提问by Brandon Yarbrough

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.

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

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.

有没有什么好方法来设置 Java/Spring/Hibernate 等价于 Rail 的测试装置?我试图找到一种方法来告诉 Hibernate“无论何时创建这个表,之后立即插入这些行”。我找不到可以添加的回调或挂钩,但也许还有另一种方法。

采纳答案by Pascal Thivent

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

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

Since Hibernate 3.1, you can include a file called import.sqlin 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.

从 Hibernate 3.1 开始,您可以import.sql在 Hibernate 的运行时类路径中包含一个调用的文件,并且在模式导出时,Hibernate 将在模式导出后执行该文件中包含的 SQL 语句。

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

此功能已在Rotterdam JBug 和 Hibernate 的 import.sql博客文章中宣布:

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 SessionFactorycreation right after the database schema generation to import data in a fresh database. You just need to add a file named import.sqlin your classpath root and set either createor create-dropas your hibernate.hbm2ddl.autoproperty.

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.sqlis 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.

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

Hibernate 有一个简洁的小功能,但它的文档不足且未知。您可以SessionFactory在数据库模式生成后立即在创建过程中执行 SQL 脚本以将数据导入到新数据库中。您只需要添加一个import.sql在类路径根目录中命名的文件,然后将create或 设置create-drop为您的 hibernate.hbm2ddl.auto属性。

现在我已经开始了查询章节,我将它用于 Hibernate Search in Action。它用一组新的数据初始化我的数据库,用于我的单元测试。JBoss Seam 在各种示例中也大量使用它。 import.sql是一个非常简单的功能,但有时非常有用。请记住,SQL 可能依赖于您的数据库(啊可移植性!)。

#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');

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

It is still not really documented.

它仍然没有真正记录下来。

回答by brabster

If you're talking about JUnit tests and using AbstractTransactionalDataSourceSpringContextTeststhere's methods you can override like onSetupBeforeTransactionthat provide a hook to pre-populate test table data etc.

如果您正在谈论 JUnit 测试并使用AbstractTransactionalDataSourceSpringContextTests,那么您可以覆盖一些方法,例如onSetupBeforeTransaction提供一个钩子来预填充测试表数据等。

回答by mmalmeida

In hibernate 3.6 the configuration that allows to run arbitrary sql commands is:

在 hibernate 3.6 中,允许运行任意 sql 命令的配置是:

hibernate.hbm2ddl.import_files

hibernate.hbm2ddl.import_files

See in http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/, noticing there is an error in the documentation: the property is import_files, with an s in the end.

参见http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/,注意到文档中有一个错误:属性是 import_files,最后是一个 s。