【问题标题】:SQL Query OK, but using Hibernate "Multiple ResultSets were returned by the query."-ErrorSQL 查询正常,但使用 Hibernate “查询返回了多个结果集。”-错误
【发布时间】:2015-10-06 07:55:59
【问题描述】:

当我在 postgesql 上直接通过 sql 执行语句时,它可以工作。

使用 java 服务/休眠时,它返回错误“查询返回了多个结果集。”

在 Eclipse 中为休眠设置 max.log 不会显示更多细节。

有人遇到过这个问题并找到了解决方案吗?提前致谢。

public List<List<String>> getProductionGroupedByTime(
        final Object[] policyTypes,
        final Object[] policyStatuses,
        final Object[] businessTypes,
        final Object[] businessChannels,
        final Object[] agents,
        final Object[] agents2,
        final Object[] roles,
        final Object[] productDetails,
        final Date beginDate,
        final Date endDate) {

    final long begin = System.currentTimeMillis();

    final Session session = sessionFactory.getCurrentSession();

    final StringBuffer sql = new StringBuffer();
    sql.append("select ");

    sql.append("cast(extract(year from coalesce(p.production_date,p.date_created)) as text) as year, ");
    sql.append("cast(extract(month from coalesce(p.production_date,p.date_created)) as text) as month, ");
    sql.append("cast(count(p.id) as text) as count, ");
    if (ArrayUtils.isNotEmpty(productDetails)) {
        sql.append("cast(sum(ipd.premium) as text) as prod, ");
    } else {
        sql.append("cast(sum(coalesce(p.manual_premium,p.premium)) as text) as prod, ");
    }
    sql.append("cast(round((sum(coalesce(p.manual_premium,p.premium)) / 100 ) + count(p.id), 2) as text) as points ");
    sql.append("from policy p ");
    sql.append(addJoinsAndWhereClause(policyTypes, policyStatuses, businessTypes, businessChannels, agents, agents2, roles, productDetails, beginDate, endDate));

    sql.append("group by ");
    sql.append("year, ");
    sql.append("month ");

    sql.append("having sum(coalesce(p.manual_premium,p.premium)) > 0 ");

    sql.append("order by ");
    sql.append("year asc, ");
    sql.append("month asc; ");

    logger.debug(sql);

    SQLQuery query = session.createSQLQuery(sql.toString());

    query = setQueryParameters(query, policyTypes, policyStatuses, businessTypes, businessChannels, agents, agents2, roles, productDetails, beginDate, endDate);

    final List<List<String>> result = DiacUtils.listOfObjectArrayToListOfListOfString(query.list());

    logger.debug((System.currentTimeMillis() - begin) + " ms");

    return result;
}

【问题讨论】:

  • 也许听起来很傻,但您是否尝试过删除查询末尾的分号,例如sql.append("month asc");

标签: java eclipse hibernate postgresql


【解决方案1】:

通常,查询末尾的分号是原因。检查this。如this other SO post 中所述,另一个原因可能是在同一查询中存在多个选择。

【讨论】:

  • 谢谢 Andrea,我认为原因一定是 hibernate / postgresql 更新,因为它在几周前工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多