Java 为什么冬眠不';无法从本机查询返回任何内容
我有一个基于Hibernate4.2和SpringBoot1.4的应用程序。我有一个非常具体的SQL查询,我不能用HQL以性能的方式建模Java 为什么冬眠不';无法从本机查询返回任何内容,java,spring,hibernate,spring-boot,Java,Spring,Hibernate,Spring Boot,我有一个基于Hibernate4.2和SpringBoot1.4的应用程序。我有一个非常具体的SQL查询,我不能用HQL以性能的方式建模 log.debug("Request to get all current Bids for station : code {}, bidType {}, versionNum {}", code, bidType, grainProAdminProperties.getPrice().getCurrentVersionNumber()); List<
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);
因此,返回的结果为空。我试图用相同的参数直接在DB中执行完全相同的请求,得到3个结果
你能预测一下为什么会出现这种情况吗?问题是(正如@GaëlMarziou所说)在一个绑定中。我正在使用Enum BidType作为此查询的输入参数,但未使用标准toString方法将其转换为字符串。您是否尝试将3个查询参数硬编码为查询中的常量,以检查其是否存在绑定问题?谢谢@GaëlMarziou!你是对的。我使用Enum BidType作为输入参数,我认为标准的toString()方法将在SQL中被调用以替换。但是买一些理由它不是真的:(你能给我解释一下这种行为的原因吗?太好了,所以你用了
name()
枚举上的方法?这可能还取决于枚举字段映射到列的方式。我使用了toString,它具有默认实现返回名称
。枚举字段的配置是@NotNull@Enumerated(EnumType.STRING)@column(name=“bid\u type”,nullable=false)private BidType BidType;
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: []