英文:
Constraint violation during maven clean install, but not when test ran independently
问题
我遇到了以下问题:
在运行maven clean install时报告了以下约束违规,但是当我单独运行测试时,测试是成功的,可能的原因是什么?
-------------------------------------------------------------------------------
测试集: com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest
-------------------------------------------------------------------------------
运行的测试: 3,失败: 0,错误: 1,跳过: 0,耗时: 0.35秒 <<< 失败!- 位于 com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest
com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest.testUpdateOfLyric 耗时: 0.069秒 <<< 错误!
org.springframework.dao.DataIntegrityViolationException: 无法执行语句;SQL [n/a];约束[idx_lyrics_song_nr_version];嵌套异常为 org.hibernate.exception.ConstraintViolationException: 无法执行语句
at com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest.testUpdateOfLyric(LyricsRepositoryTest.java:42)
Caused by: org.hibernate.exception.ConstraintViolationException: 无法执行语句
at com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest.testUpdateOfLyric(LyricsRepositoryTest.java:42)
Caused by: java.sql.SQLIntegrityConstraintViolationException: 键为'idx_lyrics_song_nr_version'的重复条目'3-1'
at com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest.testUpdateOfLyric(LyricsRepositoryTest.java:42)
而且代码如下:
@SpringBootTest(properties = { "initialize=false" })
@ActiveProfiles("devtest")
class LyricsRepositoryTest {
@Autowired
LyricsRepository lyricsRepository;
@Autowired
LyricsService lyricsService;
@BeforeEach
@AfterEach
void setUp() {
this.lyricsRepository.deleteAll();
}
@Test
@Transactional
void testUpdateOfLyric() {
final Lyric lyric1 = createRandomLyric(3);
this.lyricsRepository.save(lyric1);
this.changeLyric(lyric1);
final List<Lyric> lyric1f = this.lyricsRepository.findBySongNr(3);
Assertions.assertEquals(1, lyric1f.size());
Assertions.assertEquals(3, lyric1f.get(0).getSongNr());
Assertions.assertEquals(7, lyric1f.get(0).getVersion());
Assertions.assertEquals("EnText已更改", lyric1f.get(0).getLyricEn());
}
....
确实存在一个约束,但在此之前数据库应该已被清空。
问候,
Rick
英文:
I have the following issue:
The following constraint violation is reported during maven clean install, however when I run the test independently it is a succes, what can be the cause?
-------------------------------------------------------------------------------
Test set: com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest
-------------------------------------------------------------------------------
Tests run: 3, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.35 s <<< FAILURE! - in com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest
com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest.testUpdateOfLyric Time elapsed: 0.069 s <<< ERROR!
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [idx_lyrics_song_nr_version]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
at com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest.testUpdateOfLyric(LyricsRepositoryTest.java:42)
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
at com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest.testUpdateOfLyric(LyricsRepositoryTest.java:42)
Caused by: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '3-1' for key 'idx_lyrics_song_nr_version'
at com.gsm.GsmWeb.domain.repository.LyricsRepositoryTest.testUpdateOfLyric(LyricsRepositoryTest.java:42)
and the code is:
@SpringBootTest(properties = { "initialize=false" })
@ActiveProfiles("devtest")
class LyricsRepositoryTest {
@Autowired
LyricsRepository lyricsRepository;
@Autowired
LyricsService lyricsService;
@BeforeEach
@AfterEach
void setUp() {
this.lyricsRepository.deleteAll();
}
@Test
@Transactional
void testUpdateOfLyric() {
final Lyric lyric1 = createRandomLyric(3);
this.lyricsRepository.save(lyric1);
this.changeLyric(lyric1);
final List<Lyric> lyric1f = this.lyricsRepository.findBySongNr(3);
Assertions.assertEquals(1, lyric1f.size());
Assertions.assertEquals(3, lyric1f.get(0).getSongNr());
Assertions.assertEquals(7, lyric1f.get(0).getVersion());
Assertions.assertEquals("EnText has been changed", lyric1f.get(0).getLyricEn());
}
....
There is indeed a constraint but I shouldn't be violated as the database is emptied beforehand.
Regards,
Rick
答案1
得分: 1
我找到了,@BeforeAll 在构建过程中是在事务中的,并且需要被刷新。
添加这个解决了问题:
this.lyricsRepository.flush();
英文:
I found it, @BeforeAll during build is in transactional and needs to be flushed.
Adding this resolved the problem:
this.lyricsRepository.flush();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论