gpt4 book ai didi

java - 为什么 Hibernate 不从 native 查询返回任何内容

转载 作者:行者123 更新时间:2023-11-30 06:33:11 25 4
gpt4 key购买 nike

我有一个基于 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: []

所以返回结果为空。我尝试使用相同的参数直接在数据库中执行完全相同的请求,并且得到 3 个结果。

您能否预测一下为什么会出现这种情况?

最佳答案

问题出在(正如 @GaëlMarziou 所说)绑定(bind)中。我使用 Enum BidType 作为此查询的输入参数,但未使用标准 toString 方法将其转换为字符串。

关于java - 为什么 Hibernate 不从 native 查询返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45681296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com