JPA selects query with timestamps and date fields, cannot get results

I am trying to get records using JPA Query, which has DATE and TIMESTAMP columns in a WHERE clause. But for some reason, the date and time columns cannot extract anything from the 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 the data in 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

I initially try to get the results using EntityManager.find (class, object), but it also failed, so I tried with createNativeQuery (), which also failed. My FinTable object has these columns as Timestamp and Date.

Please enlighten the right way. :) Thank you!

+4
2

, . .

DATE TIMESTAMP.
50% SimpleDateFormat, 24Hr Hour (HH) AM/PM , ...

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

...

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

50% COL3_DATE, DATE, TIMESTAMP; TIMESTAMP .

Date - (java.sql.Date, java.util.Date Temporal.DATE).

.:)

0

, , , , , ().

, Date ( ):

query.setParameter("date", new java.util.Date(), TemporalType.DATE);

query.setParameter("date", new java.util.Date(), TemporalType.TIMESTAMP);
+6

Source: https://habr.com/ru/post/1570312/


All Articles