Reset JPA generated value between tests

Posted by Rythmic on Stack Overflow See other posts from Stack Overflow or by Rythmic
Published on 2013-10-19T20:18:32Z Indexed on 2013/10/19 21:55 UTC
Read the original article Hit count: 160

Filed under:
|
|
|
|

I'm running spring + hibernate + JUnit with springJunit4runner and transactional set to default rollback I'm using in-memory derbydb as Database. Hibernate is used as a JPA Provider and I am successfully testing CRUD kinds of stuff. However, I have a problem with JPA and the behaviour of @GeneratedValue

If I run one of my tests in isolation, two entitys are persisted with id 1 and 2. If i run the whole test suite the ids are instead 6 and 7. Spring does rollbacks just fine so there are only these two entitys in the database after addition and of course zero before. But behaviour of @GeneratedValue doesn't allow me to reliable findById unless I would return the Id from the

dao.add(Entity e) //method

I don't feel like doing that for the sake of testing, or is it a good practise to return the entity that was persisted so I should be doing it anyway?

© Stack Overflow or respective owner

Related posts about java

Related posts about spring