我在oracle数据库中执行orm查询时遇到问题,当其中一个参数是日期字符串时
这是查询:
<named-native-query name="MyProject.QueryName"
result-set-mapping="MyProject.QueryNameMapping">
<query>
<![CDATA[
select
bnumber,
to_timestamp(chargestart) as chargestart,
reportedduration,
cost,
typeofcall
from mydb@mytable t
where t.answertime is not null
and t.directorynumber = :mdn
and to_date(substr(servicestart,1,17),'dd.mm.rr HH24:MI:SS') >= to_date(:startdate, 'dd.mm.rr HH24:MI:SS')
and to_date(substr(servicestop,1,17), 'dd.mm.rr HH24:MI:SS') < to_date(:todate, 'dd.mm.rr HH24:MI:SS')
order by servicestart
]]>
</query>
</named-native-query>
<sql-result-set-mapping name="MyProject.QueryNameMapping">
<column-result name="bnumber"/>
<column-result name="chargestart"/>
<column-result name="reportedduration"/>
<column-result name="cost"/>
<column-result name="typeofcall"/>
</sql-result-set-mapping>
输入
这是我的代码:
EntityManager em = emf.createEntityManager();
List<Object[]> list = new ArrayList<Object[]>();
try{
GregorianCalendar start = startDate.toGregorianCalendar();
start.add(Calendar.HOUR_OF_DAY, -1); // because of timezone +1
GregorianCalendar end = endDate.toGregorianCalendar();
end.add(Calendar.HOUR_OF_DAY, -1); // because of timezone +1
list = ((Session) em.getDelegate()).getNamedQuery("HotelskoResenjeWS.vratiListingZaBroj")
.setParameter("mdn", account.getMdn())
.setParameter("startdate", (new SimpleDateFormat("dd.MM.yy HH:mm:ss").format(start.getTime() ) ))
.setParameter("todate", ( new SimpleDateFormat("dd.MM.yy HH:mm:ss").format( end.getTime() ) ))
.list();
}catch (Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
下午(12-23)有几小时的输入值会出现问题。当我手动运行具有相同输入的查询时,我可以在数据库中看到它们,但是当我尝试通过Java代码检索它们时,它会返回从数据库中读取的空对象列表。
为了增加混淆,代码适用于中午前几小时的输入值。 (00-12)
请帮忙。
答案 0 :(得分:0)
为什么不简单地将日历实例作为参数传递并在查询本身中使用它们而不使用该格式:
and to_date(substr(servicestart,1,17),'dd.mm.rr HH24:MI:SS') >= :startdate
and to_date(substr(servicestop,1,17), 'dd.mm.rr HH24:MI:SS') < :todate
和java部分:
list = ((Session) em.getDelegate()).getNamedQuery("HotelskoResenjeWS.vratiListingZaBroj")
.setParameter("mdn", account.getMdn())
.setParameter("startdate", start, TemporalType.TIMESTAMP)
.setParameter("todate", end, TemporalType.TIMESTAMP)
.list();