为什么Hibernate没有从本机查询返回任何东西
我有一个基于Hibernate 4.2和Spring Boot 1.4的应用程序。我有一个非常具体的SQL查询,我无法用高性能的方式用HQL进行建模。为什么Hibernate没有从本机查询返回任何东西
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所说)在一个绑定。我使用Enum BidType作为此查询的输入参数,但未使用标准toString方法将其转换为字符串。
好,所以你在你的枚举上使用了'name()'方法吗?它也可能取决于你的枚举字段如何映射到列 –
我已经使用了具有默认实现'return name'的toString,Enum字段的配置是'@NotNull @Enumerated(EnumType.STRING) @Column(name =“bid_type”,nullable = false) private BidType bidType;' –
您是否试图在查询中将您的3个查询参数硬编码为常量以检查它不是绑定问题? –
谢谢@GaëlMarziou!你是对的。我使用Enum BidType作为输入参数,我认为将调用标准的toString()方法在SQL中进行替换。但请买一些原因它不是真的:(你可以请为我解释这种行为的原因吗? –