hibernate not updating data in functional test

65 views
Skip to first unread message

sas

unread,
Sep 18, 2011, 12:37:45 PM9/18/11
to play-framework
I have the following functional test

@Test
public void deleteEventTest() {

final Long originalCount = Event.count();
final Event originalNextEvent = Event.find("order by date
desc").first();

Response response = DELETE("/event/" + originalNextEvent.id);
assertStatus(302, response);
assertHeaderEquals("Location", "/", response);

//originalNextEvent.delete();
//JPA.em().flush();
//originalNextEvent.em().flush();

assertEquals("There should be one less event",
originalCount-1, Event.count());

assertNotSame("The next event must have changed",
originalNextEvent,
Event.find("order by date desc").first()
);

// this one passes
final Event deletedEventByCondition = Event.find("id = ?",
originalNextEvent.id).first();
assertNull("The event should have been deleted",
deletedEventByCondition);

// this one fails
final Event deletedEventById =
Event.findById(originalNextEvent.id);
assertNull("The event should have been deleted",
deletedEventById);

}

everything works fine, but the last assertion

it seems like hibernate keeps a cache of the deleted object and
wouldn't release it

I checked the db and right after the call to DELETE("/event".. the
record is effectively deleted

If instead of calling the DELETE method, I issue the delete myself
everything works

I've commented a couple of things I tried (flushing the em)

Getting the object by a condition works ok, but getting it by id
doesn't work...

any idea??

saludos

sas





Keith Swallow

unread,
Sep 18, 2011, 12:47:18 PM9/18/11
to play-fr...@googlegroups.com
Hi Sas,

I've had a similar issue. I think it is a contrived problem because the functional tests are not actually separate from Play even through they pretend to be. The recipe below has worked for me in my code, you might try it on yours.

final Event deletedEventById = Event.findById(originalNextEvent.id);
deletedEventById.refresh();  // the important line
assertNull("The event should have been deleted", deletedEventById);

Please note that I don't delete things, I just update their status to deleted so the null check may not work. Is there an internal thing in hibernate that you can look at?  

myDeletedModel.isDeleted();  // true if deleted

Best,

Keith

sas

unread,
Sep 18, 2011, 1:55:42 PM9/18/11
to play-framework
I think I can be confident that this issue won't happen between two
different requests... right?

I tried calling originalNextEvent.refresh() and also
deletedEventById.refresh and they both give me the following error:

Error, the test has not been properly run. Check the server logs, or
open this test in another browser tab to see the error page.

and the error:

A javax.persistence.EntityNotFoundException has been caught, No row
with the given identifier exists: [models.Event#56]
In /test/DeleteEventTest.java, line 39 :
originalNextEvent.refresh();
Show trace

javax.persistence.EntityNotFoundException: No row with the given
identifier exists: [models.Event#56]
at
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:
1196)
at
org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:
745)
at
org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:
713)


So, the error is fine, it accurately tells what's really happening...

But I still can't find a more confortable solution. So far now I'll
keep using the find("id = ?"... version

thanks a lot

saludos

sas
Reply all
Reply to author
Forward
0 new messages