为什么Hibernate不会从Native Query返回任何内容

时间:2017-08-14 19:00:39

标签: java spring hibernate spring-boot

我有一个基于Hibernate 4.2和Spring Boot 1.4的应用程序。我有一个非常具体的SQL查询,我无法以高效的方式使用HQL进行建模。

log.debug("Request to get all current Bids for station : code {}, bidType {}, versionNum {}", code, bidType, grainProAdminProperties.getPrice().getCurrentVersionNumber());

List<Object[]> result = sessionFactory.getCurrentSession().createSQLQuery(
        "select bid.*, tp.price as tp_price, tp.price_nds as tp_priceNds " +
        "from bid, transportation_price tp, station_location lts, partner part, station stat " +
        "where " +
        "   bid.is_active = true and" +
        "   bid.archive_date is null and " +
        "   part.id = bid.elevator_id and " +
        "   part.station_id = stat.id and " +
        "   lts.region_id = stat.region_id and " +
        "   lts.district_id = stat.district_id and " +
        "   (stat.locality_id is null or " +
        "   lts.locality_id = stat.locality_id) and " +
        "   ((cast(tp.station_from_code as text) = lts.code and " +
        "     cast(tp.station_to_code as text) = cast(:code as text)) " +
        " or " +
        "    (cast(tp.station_to_code as text) = lts.code and " +
        "     cast(tp.station_from_code as text) = cast(:code as text))) and " +
        "    cast(bid.bid_type as text) like cast(:bidType as text) and " +
        "    cast(tp.version_number as int) = cast(:versionNumber as int)").
        setResultTransformer(
            new ResultTransformer() {
                @Override
                public Object transformTuple(Object[] tuple, String[] aliases) {
                    log.warn("Transform tuple: {}, aliases {}", tuple, aliases);
                    return null;
                }

                @Override
                public List transformList(List collection) {
                    return collection;
                }
            }
        ).
        setParameter("code", code).
        setParameter("versionNumber", grainProAdminProperties.getPrice().getCurrentVersionNumber()).
        setParameter("bidType", bidType).
        list();

    log.debug("Result of request: {}", result);

在日志文件中我可以看到:

Request to get all current Bids for station : code 865065, bidType BUY, versionNum 2
Hibernate: select bid.*, tp.price as tp_price, tp.price_nds as tp_priceNds from bid, transportation_price tp, station_location lts, partner part, station stat where    bid.is_active = true and   bid.archive_date is null and    part.id = bid.elevator_id and    part.station_id = stat.id and    lts.region_id = stat.region_id and    lts.district_id = stat.district_id and    (stat.locality_id is null or    lts.locality_id = stat.locality_id) and    ((cast(tp.station_from_code as text) = lts.code and      cast(tp.station_to_code as text) = cast(? as text))  or     (cast(tp.station_to_code as text) = lts.code and      cast(tp.station_from_code as text) = cast(? as text))) and     cast(bid.bid_type as text) like cast(? as text) and     cast(tp.version_number as int) = cast(? as int)
Result of request: []

因此返回的结果为空。我试图在DB中使用相同的参数直接执行完全相同的请求,我得到3个结果。

你能预测为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

问题是(正如@GaëlMarziou所说)的约束力。我使用Enum BidType作为此Query的输入参数,但是它没有使用标准的toString方法转换为字符串。