I have the following code fragments:
$Stateless
public testEJB
{
@PersistenceContext(unitName = "testunit")
private EntityManager entityManager;
public void modifyTest() { //update a record
deleteTest();
addTest();
}
public deleteTest() //delete a record from db
{
entityManager.remove(..);
}
public addTest() //insert a record to db
{
entityManager.persist(..);
}
}
The deleteTest() and addTest() methods both work fine. However, the modifyTest() does not work! It keeps complaining about unique key constraints. Seems to me that the deleteTest() call does not really remove the original record and therefore the addTest() can not execute properly because it thinks another record with an exisiting key is being inserted. Or maybe the entityManager is still seeing the stale data? Anyway, if I add the entityManager.flush() then it works.
public void modifyTest() { //update a record from db
deleteTest();
[b]entityManager.flush();[/b]
addTest();
}
Can someone please explain to me why this is happening? Shouldn't the flush happen automatically? when deleteTest() is called? By the way, I am using the hibernate entitymanager.
Thanks a lot for your help!
Joanne
[Message sent by forum member 'jmao' (jmao)]
http://forums.java.net/jive/thread.jspa?messageID=224608