H2数据库的TIMESTAMP列的默认值 [英] H2 database default value of TIMESTAMP column
问题描述
我正在编写与H2数据库的集成测试. 我的数据库(生成的)初始化包含此脚本(因为生成的联接表没有此列):
I am writing integration tests with H2 database. My database (generated) initialization include this script (because generated join table does not have this column):
ALTER TABLE INT_USR ADD IU_INSDTTM TIMESTAMP DEFAULT NOW();
这是我创建记录的方式:
This is how I create records:
Integration integrationOne = createIntegration(firstId, "FIRST");
Integration integrationTwo = createIntegration(secondId, "SECOND");
flushAndClear();
userService.logRecentIntegration(integrationOne.getId(), user.getId());
flushAndClear();
userService.logRecentIntegration(integrationTwo.getId(), user.getId()); //1
logRecentIntegrations(..,..)方法仅调用DAO,而dao则这样做:
The method logRecentIntegrations(.., ..) just calls the DAO and the dao does this:
Query query = entityManager.createNativeQuery(
"INSERT INTO INT_USR (USR_ID, INT_ID) VALUES (?, ?)");
query.setParameter(1, userId)
.setParameter(2, integrationId);
query.executeUpdate();
后来在我的考试中:
Query query = entityManager.createNativeQuery(
"SELECT * FROM INT_USR ORDER BY IU_INSDTTM");
List resultList = query.getResultList();
当我在resultList中调试此测试时,有两个记录(正确),但是它们具有相同的时间戳.即使当我在标记为//1的行上插入断点并等待一会儿时,所以插入之间的时间间隔也会很大. (Thread.sleep-相同的结果)
When I debug this test in resultList there are two records (correct) but they have same timestamp. Even when I inserted a breakpoint on line marked //1 and waited a while - so the time gap between inserts would be significant. (Thread.sleep - same result)
我试图将SQL脚本修改为
I tried to modify the SQL script to
ALTER TABLE INT_USR ADD IU_INSDTTM TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
但结果相同.为什么两个结果都有相同的时间戳?
But with same result. Why both results have same timestamp?
推荐答案
如所述,函数CURRENT_TIMESTAMP 在事务中始终返回相同的值.此行为与其他数据库(例如PostgreSQL)相匹配.
As documented, the function CURRENT_TIMESTAMP always returns the same value within a transaction. This behavior matches other databases, for example PostgreSQL.
这篇关于H2数据库的TIMESTAMP列的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!