I switched my application’s entity manager factory from spring-managed to container-managed and have run into a problem. While creating an entity, there are times when I have to query for it within the same transaction that it was originally created in, before it has been flushed automatically at the end. In spring, the entity manager would (I believe) automatically flush the context before running find and be able to return the correct object, but in jboss the find method returns null.
Here is a sample of the problem:
@PersistenceContext
private EntityManager entityManager;
private void test(){
Person person = new Person("firstname", "lastname", "email", "user", "pass", Person.PersonType.admin);
entityManager.persist(person);
assert person.getId() != null;
Person person2 = entityManager.find(Person.class, person.getId());
assert person2 != null; //fail in jboss, ok in Spring
}
If I try to add an entityManager.flush() call in between persist() and find() methods, jboss throws a javax.persistence.TransactionRequiredException: no transaction is in progress error, regardless of whether I throw @Transactional annotations around the method/class running the test.
My persistence.xml file sets <property name="org.hibernate.FlushMode" value="auto"/>, but it doesn’t seem to be working.
Is there some configuration property that I missed to tell jboss to flush its context to the database before running a Find? Or am I ordering my statements incorrectly?
I’ve put my persistence.xml and my-ds.xml files on pastebin.
Thanks for the help.
Edit
Resolved. See comments below
Soon after uploading my spring-data.xml, I realized that I forgot the bean. After adding that, the @Transactional tag worked as expected and the problem was resolved.