java 在 h2 中重置自动增量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10065386/
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
Resetting autoincrement in h2
提问by marsellvov
I'm testing a controller that returns a json response but the tests fail after the first time because the h2 database does not reset the auto increment id. Using fixtures or creating objects manually has a same problem.
我正在测试一个返回 json 响应的控制器,但在第一次之后测试失败,因为 h2 数据库没有重置自动增量 ID。使用夹具或手动创建对象也有同样的问题。
@Before
public void setUp() {
Fixtures.deleteAllModels();
Fixtures.loadModels("data.yaml");
}
How to solve this problem?
如何解决这个问题呢?
回答by dwi2
Start your play app, fire up browser with this url (if you run play app locally):
启动您的播放应用程序,使用此网址启动浏览器(如果您在本地运行播放应用程序):
http://localhost:9000/@db
Enter your h2 db, and type the command below and run:
输入您的 h2 db,然后键入以下命令并运行:
ALTER TABLE <table_name> ALTER COLUMN <column_name> RESTART WITH 1
If you'd like to do this programmatically, Fixtures.executeSQL()
might be useful
如果您想以编程方式执行此操作,Fixtures.executeSQL()
可能会有用
For more information, check http://www.h2database.com/html/grammar.html#alter_table_alter
有关更多信息,请查看http://www.h2database.com/html/grammar.html#alter_table_alter
回答by Benjamin Slabbert
If you are using Spring, I found this blog postwhich suggests using @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
如果您使用的是 Spring,我发现这篇博客文章建议使用@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
So that each test uses a new application context, thereby resetting the db increments.
这样每个测试都使用新的应用程序上下文,从而重置 db 增量。