I have a Spring (Boot) application on which I'm trying to use Hibernate Envers.
The following bar
function throws an IllegalStateException: EntityManager is closed
, while the foo
function works flawlessly:
@Service
public class FoobarService {
private final EntityManager entityManager;
@Autowired
public FoobarService(EntityManager entityManager) {
this.entityManager = entityManager;
}
public Iterable<Foobar> foo() {
return entityManager.createQuery("select f from Foobar f", Foobar.class).getResultList();
}
public Iterable<Foobar> bar() {
System.out.println(entityManager.isOpen()); // <--- Returns true!
AuditReader auditReader = AuditReaderFactory.get(entityManager);
AuditQueryCreator queryCreator = auditReader.createQuery();
AuditQuery query = queryCreator.forRevisionsOfEntity(Foobar.class, true);
return query.getResultList();
}
}
Does anybody know why it claims it's closed when the foo
function clearly works (as stated above)?
PS: I've omitted the maven dependencies and entity mapping for brevity, as they're all working.
EDIT:
There's something really weird I can't figure out. Within the bar
function, we can see that:
entityManager.isOpen()
returns true((Session) entityManager.getDelegate()).isOpen()
returns false
The AuditReaderFactory.get(EntityManager entityManager)
function uses the second, this is why it complains the session is closed when it's not. I don't quite get why the delegate
is closed, so:
- Why does the the factory uses the
entityManager
's delegate instead? - Why the delegate is even closed?