我想从Kotlin中的DB中选择一个值,但得到"Caused by: java.lang.IllegalStateException: No element found in 'only'“错误。
我已经编写了一个类,并且使用了map作为RowMapper
class MarketplaceGetEffectiveRateQuery(private val handle: Handle) {
fun run(resourceUUID: UUID,subscriptionUuid: UUID): BigDecimal {
return handle.createQuery(GET_EFFECTIVE_RATE_QUERY)
.bind("resourceUUID", resourceUUID)
.bind("ms_subscription_id", subscriptionUuid)
.map(EffectiveRateMapper())
.findOnly()
}
} override fun map(rs: ResultSet, ctx: StatementContext): BigDecimal {
return rs.getBigDecimal("effective_rate")
}
}发布于 2019-08-05 18:03:55
我假设您使用的是JDBI。如果是这样,那么这是绝对正确的行为。
public interface ResultIterable<T> extends Iterable<T> {
/**
* Get the only row in the result set.
* @throws IllegalStateException if zero or multiple rows are returned
* @return the object mapped from the singular row in the results
*/
default T findOnly() { ... }
}注意:如果返回零个或多个IllegalStateException行,则@引发
因此,只有当您绝对确定查询始终生成单个结果时,才应使用findOnly()
https://stackoverflow.com/questions/57355994
复制相似问题