我有以下问题:
这是我给定的存储库实现
public class SearchRepoImpl<T, ID extends Serializable> implements SearchRepo<T,ID> {
private final EntityManager em;
@Override
public Stream<T> findStreamBySpec(Specification<T> where, Integer limit, Class<T> clazz) {
final Class<T> domainClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<T> query = builder.createQuery(domainClass);
applySpecificationToCriteria(where, domainClass, query); // this method does the necessary preprocessing and is of no interest
return em.createQuery(query)
.getResultStream()
.limit(limit.longValue());
}
}
但是,当我使用
searchRepo.findStreamBySpec(spec, 100, MyClass.class).forEach(e-> {...})
调用此实现时,出现以下异常:
org.hibernate.exception.GenericJDBCException:无法确定是否 由于异常调用 isBeforeFirst 或 isAfterLast()
我已经设法将问题追查到 DB2 驱动程序中的反编译代码行:
private final void checkThatResultSetTypeIsScrollable() throws SQLException {
if (this.resultSetType_ == ResultSet.FORWARD_ONLY) {
throw hd.a(this, this.agent_.logWriter_, ErrorKey.METHOD_NOT_ALLOWED_ON_FORWARD_ONLY_CURSOR, "10900");
}
}
依次从 HikariProxyResultSet 调用此方法
public boolean isBeforeFirst() throws SQLException {
this.agent_.systemMonitor_.c();
boolean var2;
try {
if (this.agent_.loggingEnabled()) {
this.agent_.logWriter_.traceEntry(this, "isBeforeFirst");
}
this.checkForClosedResultSet();
this.checkThatResultSetTypeIsScrollable();
//^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ calls above method checkThatResultSetTypeIsScrollable()
boolean var1 = this.isBeforeFirstX();
if (this.agent_.loggingEnabled()) {
this.agent_.logWriter_.traceExit(this, "isBeforeFirst", var1);
}
var2 = var1;
} finally {
this.agent_.systemMonitor_.a(this.statement_);
}
return var2;
}
从
调用 private boolean isResultSetEmpty() {
try {
return currentPosition == 0 && !getResultSet().isBeforeFirst() &&
!getResultSet().isAfterLast();
//^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ calls above method isBeforeFirst()
}
catch (SQLException e) {
throw getSession().getFactory().getSQLExceptionHelper().convert(
e,
"Could not determine if resultset is empty due to exception calling isBeforeFirst or isAfterLast()"
);
}
}
我的问题是,有没有办法向 Hibernate、HikariCP 和/或 DB2 提示或建议我不想将
ResultSet
设置为 FORWARD_ONLY
或者我不想使用那种 Scrollable结果。
你需要从服务调用你的存储库并用@Transactional包装它