我正在尝试使用H2嵌入式数据库在Spring Boot2.1.4中进行junit测试。我正面临一个关于序列的特殊问题。所以根据spring boot和h2教程,我有一个schema.sql脚本来生成我的相关表:
create sequence seq_test increment by 50;我可以看到创建序列是从调试日志中调用的。
为了测试序列是否正常工作,我对它运行了nextval(),但得到了一个表seq_test is not created错误。这只发生在序列上,当我查询一个普通的表时,我得到了一个正确的结果。
测试用例:
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest
@Import({EmbeddedRedis.class, EmbeddedRabbitMQ.class})
@Slf4j
public class ProfileCreationTests {
@Autowired
private EntityManager em;
@Test
public void profileCreationTest() {
Query query = em
.createNativeQuery("SELECT NEXTVAL(seq_test)");
log.info(query.getSingleResult().toString());
log.info(em.createNativeQuery("SELECT count(*) FROM tbl_test").getSingleResult().toString());
}
}第一行错误被删除,如果我将其注释掉,第二个查询运行正常。
app config: spring: profiles: test h2: console: enabled: true jpa: hibernate: use-new-id-generator-mappings: true
发布于 2019-04-23 15:44:06
试试Query query = em.createNativeQuery("call next value for seq_test");
h2提供了不同的compatibility modes,它们获取下一个值的语法也不同。当没有选择兼容模式时,我认为上面的语法很好。
https://stackoverflow.com/questions/55803548
复制相似问题