我這是一個奇怪的問題,但它去了!Hibernate的flush()方法中的無限遞歸
放在上下文中: 我正在執行我的JUnit測試,我正在測試一個名爲songJPA的類。我創建了一首歌曲(OK),然後我創建了一個具有相同名稱的歌曲,並期望有一個例外(OK),最後我想要刪除第一首歌曲創建(在下一次執行測試時間中不會有錯誤)。
問題是,當我單獨執行DeleteSongTest()時,它工作並刪除我的歌曲,但是當我執行它們全部時,它不起作用,它進入無限遞歸。我認爲它在flush()方法中發生,我嘗試調試但不可能。
我粘貼所有我認爲正在工作的類。
JUnit測試類:
public class SongCRUDTest {
private SongJPA testSong1;
private SongJPA testSong2;
private SongJPAJpaController slc;
@Before
public void setUp() {
testSong1= new SongJPA("TestTitle", 120, 19, new SongInfoJPA(), "TestArtist", Genre.CLASSICAL);
testSong2= new SongJPA("TestTitle", 122, 12, new SongInfoJPA(), "TestArtist2", Genre.BLUES);
slc = new SongJPAJpaController();
}
@Test
public void A_createSong(){
slc.create(testSong1);
}
@Test(expected = PersistenceException.class)
public void B_createDuplicatedTestSong() {
slc.create(testSong2);
}
@Ignore
@Test
public void C_deleteSong() throws NonexistentEntityException{
SongJPA songToDelete= slc.findSongJPAByTitle("TestTitle");
if(songToDelete!=null)
slc.destroy(songToDelete.getId());
}
的銷燬方法,只有當我單獨執行工作。 (從NetBeans的模板)
public void destroy(int id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
SongJPA songJPA;
try {
songJPA = em.getReference(SongJPA.class, id);
songJPA.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The songJPA with id " + id + " no longer exists.", enfe);
}
em.remove(songJPA);
em.flush();
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
版1:創建()方法
public void create(SongJPA songJPA) { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); em.persist(songJPA); em.getTransaction().commit(); } finally { if (em != null) { em.close(); } } }
秒鐘後
54587 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1205, SQLState: 41000 54587 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Lock wait timeout exceeded; try > restarting transaction
TIA求助!!!!
你可以顯示'create()'方法嗎? – axtavt
當然@axtavt – migueloop