带有时间戳和日期字段的JPA选择查询无法检索结果 [英] JPA select query with timestamp and date fields fail to retrieve results

查看:954
本文介绍了带有时间戳和日期字段的JPA选择查询无法检索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JPA Query在WHERE子句中具有DATE和TIMESTAMP列的记录。但不知何故日期和时间戳列无法从数据库中检索任何东西。

代码段:

  String sql =Select F. * from FIN_TABLE F其中F.COL1_NUM =:COL1和F.COL2_TIMESTAMP =:COL2和F.COL3_DATE =:COL3; 
查询查询= JPAentityManager.createNativeQuery(sql);
query.setParameter(COL1,123);
//java.sql.Timestamp:2014-10-29 12:00:00.0
query.setParameter(COL2,new java.sql.Timestamp(new java.sql.Date(new SimpleDateFormat (MMM dd yyyy HH:mm:sssa)。parse(OCT 29 2014 12:00:000AM)。getTime())。getTime()));
//java.sql.Date:2014-10-29
query.setParameter(COL3,new java.sql.Date(新的SimpleDateFormat(MMM dd yyyy HH:mm:sssa)) .parse(2014年10月29日12:00:000AM).getTime()));

列表< FinTable> result =(List< FinTable>)query.getResultList();

Oracle中的数据是:

 
COL1_NUM COL2_TIMESTAMP COL3_DATE
123 29-OCT-14 12.00.00.000000000 AM 26-Nov-14
456 29- OCT-14 12.00.00.000000000 AM 26-Nov-14

我最初试图使用EntityManager.find(Class,Object) ,但它也失败了,所以我尝试了createNativeQuery(),这也失败了。
我的FinTable实体具有这些列作为时间戳和日期。



请指出正确的方法。 :)
Thanks !!

解决方案

感谢您的回复。
问题已解决。



我正确使用DATE和TIMESTAMP对象。


50%的问题是使用SimpleDateFormat解析,其中我提到了24小时格式(HH)和AM / PM在一起,使用的正确解析器是...
p>


MMM dd yyyy hh :mm:sssa// 2014年10月29日12:00:000AM


而不是...


MMM dd yyyy HH :mm:sss a // 2014年10月29日00:00:000

问题的剩余50%是COL3_DATE错误地定义为DATE,但最初是TIMESTAMP;将其更改为TIMESTAMP解决了问题。然而,使用Date仍然是任何方法的问题(java.sql.Date,带有Temporal.DATE的java.util.Date)。



感谢您的贡献。 :)

I am trying to fetch records using JPA Query which has DATE and TIMESTAMP columns in WHERE clause. But somehow date and timestamp columns fail to retrieve anything from database.

Code Segment:

String sql = "Select F.* from FIN_TABLE F where F.COL1_NUM = :COL1 and F.COL2_TIMESTAMP =:COL2 and F.COL3_DATE =:COL3";
Query query = JPAentityManager.createNativeQuery(sql);
query.setParameter("COL1", 123);
//java.sql.Timestamp:2014-10-29 12:00:00.0
query.setParameter("COL2", new java.sql.Timestamp(new java.sql.Date(new SimpleDateFormat("MMM dd yyyy HH:mm:sssa").parse("OCT 29 2014 12:00:000AM").getTime()).getTime()));
//java.sql.Date:2014-10-29
query.setParameter("COL3", new java.sql.Date(new SimpleDateFormat("MMM dd yyyy HH:mm:sssa").parse("OCT 29 2014 12:00:000AM").getTime()));

List<FinTable> result =  (List<FinTable>)query.getResultList();

And Data in Oracle is:

COL1_NUM    COL2_TIMESTAMP                      COL3_DATE
123         29-OCT-14 12.00.00.000000000 AM     26-Nov-14
456         29-OCT-14 12.00.00.000000000 AM     26-Nov-14

I am originally trying to retrieve results using EntityManager.find(Class, Object), but it was also failing so i tried with createNativeQuery(), which also fails to succeed. My FinTable entity has these columns as Timestamp and Date.

Please enlighten the right way. :) Thanks !!

解决方案

Thanks for replying. Problem is resolved.

I was correctly using the DATE and TIMESTAMP objects.
50% of the problem problem was with the SimpleDateFormat parsing where I had mentioned the 24Hr Hour format (HH) and AM/PM together, the correct parser to use was...

"MMM dd yyyy hh:mm:sssa" //OCT 29 2014 12:00:000AM

and not...

"MMM dd yyyy HH:mm:sssa" //OCT 29 2014 00:00:000

Remaining 50% of the problem was with column COL3_DATE which was mistakenly defined as DATE but was originally a TIMESTAMP; changing it to TIMESTAMP resolved the problem.

However, working with Date is still the issue by any approach (java.sql.Date, java.util.Date with Temporal.DATE).

Thanks for contributing. :)

这篇关于带有时间戳和日期字段的JPA选择查询无法检索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆