Hello
I have a problem with Hibernate Envers, we use Hibernate v.5 and Hibernate Envers in our persistence bundles within a large set of OSGi bundles that cater to an upstream platform. Our Hibernate stuff works generally fine, but Envers is giving me quite a headache.
Here’s my code, or actually three versions of what I’ve tried, and all of them throw the exception during the same call to getResultList:
/**
* the very first try
*/
@SuppressWarnings("unchecked")
public List<ISomeKindOfPerson> searchDeletedPeople(IProcessContext processContext, String firstName, String lastName, PartialDate birthDate)
throws OurCheckedWhateverWentWrongException, UpstreamCheckedWeMessedUpOurProvidersException, NonUniqueResultException, NoResultException {
final Map<UUID, PersonSearchRecord> uniquePersonSearchRecords = new HashMap<>();
final List<ISomeKindOfPerson> auditedPeople = new ArrayList<>();
if (StringUtils.isBlank(firstName) && StringUtils.isBlank(lastName) && birthDate == null)
return Collections.emptyList();
try (SessionImplementor em = Persistence.createEM(processContext)) {
final AuditReader reader = AuditReaderFactory.get(em);
final AuditQuery personSearchRecordQuery = reader.createQuery().forRevisionsOfEntity(PersonSearchRecord.class, true, true);
personSearchRecordQuery.add(AuditEntity.revisionType().eq(RevisionType.DEL));
if (StringUtils.isNotBlank(firstName)) {
personSearchRecordQuery.add(AuditEntity.property(PersonSearchRecord.COLUMN_NAMES.FIRST).ilike(firstName, MatchMode.ANYWHERE));
}
if (StringUtils.isNotBlank(lastName)) {
personSearchRecordQuery.add(AuditEntity.property(PersonSearchRecord.COLUMN_NAMES.LAST).ilike(lastName, MatchMode.ANYWHERE));
}
if (birthDate != null) {
personSearchRecordQuery.add(AuditEntity.property(PersonSearchRecord.COLUMN_NAMES.BIRTHDATE).eq(new ProPartialDate(birthDate)));
}
final List<PersonSearchRecord> results = personSearchRecordQuery.getResultList(); // luckily this works without any explicit casting in my limited click-testing so far
for (final PersonSearchRecord personSearchRecord : results) {
if (!uniquePersonSearchRecords.containsKey(personSearchRecord.getIdent())) {
uniquePersonSearchRecords.put(personSearchRecord.getUuid(), personSearchRecord);
}
}
for (final Map.Entry<UUID, PersonSearchRecord> personSearchRecord : uniquePersonSearchRecords.entrySet()) {
final AuditQuery personQuery = reader.createQuery().forRevisionsOfEntity(Person.class, true, true);
personQuery.add(AuditEntity.revisionType().eq(RevisionType.DEL));
personQuery.add(AuditEntity.id().eq(personSearchRecord.getValue().getPersonId()));
final List<Person> queryResults = personQuery.getResultList(); // <------- throws java.sql.SQLException: Invalid state, the ResultSet object is closed.
if (CollectionUtils.isNotEmpty(queryResults))
auditedPeople.addAll(queryResults);
}
} catch (final HibernateException e) {
throw new OurCheckedWhateverWentWrongException(e);
}
return auditedPeople;
}
/**
* the second try
*/
@SuppressWarnings("unchecked")
public List<ISomeKindOfPerson> searchDeletedPeople(IProcessContext processContext, String firstName, String lastName, PartialDate birthDate)
throws OurCheckedWhateverWentWrongException, UpstreamCheckedWeMessedUpOurProvidersException, NonUniqueResultException, NoResultException {
final Map<UUID, PersonSearchRecord> uniquePersonSearchRecords = new HashMap<>();
final List<ISomeKindOfPerson> auditedPeople = new ArrayList<>();
if (StringUtils.isBlank(firstName) && StringUtils.isBlank(lastName) && birthDate == null)
return Collections.emptyList();
try (SessionImplementor em = Persistence.createEM(processContext)) {
final AuditReader personSearchRecordReader = AuditReaderFactory.get(em);
final AuditQuery personSearchRecordQuery = personSearchRecordReader.createQuery().forRevisionsOfEntity(PersonSearchRecord.class, true, true);
personSearchRecordQuery.add(AuditEntity.revisionType().eq(RevisionType.DEL));
if (StringUtils.isNotBlank(firstName)) {
personSearchRecordQuery.add(AuditEntity.property(PersonSearchRecord.COLUMN_NAMES.FIRST).ilike(firstName, MatchMode.ANYWHERE));
}
if (StringUtils.isNotBlank(lastName)) {
personSearchRecordQuery.add(AuditEntity.property(PersonSearchRecord.COLUMN_NAMES.LAST).ilike(lastName, MatchMode.ANYWHERE));
}
if (birthDate != null) {
personSearchRecordQuery.add(AuditEntity.property(PersonSearchRecord.COLUMN_NAMES.BIRTHDATE).eq(new ProPartialDate(birthDate)));
}
final List<PersonSearchRecord> results = personSearchRecordQuery.getResultList(); // luckily this works without any explicit casting in my limited click-testing so far
for (final PersonSearchRecord personSearchRecord : results) {
if (!uniquePersonSearchRecords.containsKey(personSearchRecord.getIdent())) {
uniquePersonSearchRecords.put(personSearchRecord.getUuid(), personSearchRecord);
}
}
} catch (final HibernateException e) {
throw new OurCheckedWhateverWentWrongException(e);
}
try (SessionImplementor em = Persistence.createEM(processContext)) {
final AuditReader personReader = AuditReaderFactory.get(em);
for (final Map.Entry<UUID, PersonSearchRecord> personSearchRecord : uniquePersonSearchRecords.entrySet()) {
final AuditQuery personQuery = personReader.createQuery().forRevisionsOfEntity(Person.class, true, true);
personQuery.add(AuditEntity.revisionType().eq(RevisionType.DEL));
personQuery.add(AuditEntity.id().eq(personSearchRecord.getValue().getPersonId()));
final List<Person> queryResults = personQuery.getResultList(); // <------- throws java.sql.SQLException: Invalid state, the ResultSet object is closed.
if (CollectionUtils.isNotEmpty(queryResults))
auditedPeople.addAll(queryResults);
}
} catch (final HibernateException e) {
throw new OurCheckedWhateverWentWrongException(e);
}
return auditedPeople;
}
/**
* the third try
*/
@SuppressWarnings("unchecked")
public List<ISomeKindOfPerson> searchDeletedPeople(IProcessContext processContext, String firstName, String lastName, PartialDate birthDate)
throws OurCheckedWhateverWentWrongException, UpstreamCheckedWeMessedUpOurProvidersException, NonUniqueResultException, NoResultException {
final Map<UUID, PersonSearchRecord> uniquePersonSearchRecords = new HashMap<>();
final List<ISomeKindOfPerson> auditedPeople = new ArrayList<>();
if (StringUtils.isBlank(firstName) && StringUtils.isBlank(lastName) && birthDate == null)
return Collections.emptyList();
try (SessionImplementor em = Persistence.createEM(processContext)) {
final AuditReader personSearchRecordReader = AuditReaderFactory.get(em);
final AuditQuery personSearchRecordQuery = personSearchRecordReader.createQuery().forRevisionsOfEntity(PersonSearchRecord.class, true, true);
personSearchRecordQuery.add(AuditEntity.revisionType().eq(RevisionType.DEL));
if (StringUtils.isNotBlank(firstName)) {
personSearchRecordQuery.add(AuditEntity.property(PersonSearchRecord.COLUMN_NAMES.FIRST).ilike(firstName, MatchMode.ANYWHERE));
}
if (StringUtils.isNotBlank(lastName)) {
personSearchRecordQuery.add(AuditEntity.property(PersonSearchRecord.COLUMN_NAMES.LAST).ilike(lastName, MatchMode.ANYWHERE));
}
if (birthDate != null) {
personSearchRecordQuery.add(AuditEntity.property(PersonSearchRecord.COLUMN_NAMES.BIRTHDATE).eq(new ProPartialDate(birthDate)));
}
final List<PersonSearchRecord> results = personSearchRecordQuery.getResultList(); // luckily this works without any explicit casting in my limited click-testing so far
for (final PersonSearchRecord personSearchRecord : results) {
if (!uniquePersonSearchRecords.containsKey(personSearchRecord.getIdent())) {
uniquePersonSearchRecords.put(personSearchRecord.getUuid(), personSearchRecord);
}
}
} catch (final HibernateException e) {
throw new OurCheckedWhateverWentWrongException(e);
}
for (final Map.Entry<UUID, PersonSearchRecord> personSearchRecord : uniquePersonSearchRecords.entrySet()) {
try (SessionImplementor em = Persistence.createEM(processContext)) {
final AuditReader personReader = AuditReaderFactory.get(em);
final AuditQuery personQuery = personReader.createQuery().forRevisionsOfEntity(Person.class, true, true);
personQuery.add(AuditEntity.revisionType().eq(RevisionType.DEL));
personQuery.add(AuditEntity.id().eq(personSearchRecord.getValue().getPersonId()));
final List<Person> queryResults = personQuery.getResultList(); // <------- throws java.sql.SQLException: Invalid state, the ResultSet object is closed.
if (CollectionUtils.isNotEmpty(queryResults))
auditedPeople.addAll(queryResults);
} catch (final HibernateException e) {
throw new OurCheckedWhateverWentWrongException(e);
}
}
return auditedPeople;
}
Any pointers to what I’m doing wrong are very appreciated
Many thanks in advance for any help
Cheers
Zac