我这是一个奇怪的问题,但它去了!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