2012-04-28 131 views
2

其中的Oracle表的看起來像這樣,我沒有改變這個的選項:Oracle日期和休眠/ JPA映射

REPORTING_PERIOD | REPORTING_DATE (Oracle DATE type) 
------------------------------- 
1140    01-FEB-12 
1139    01-JAN-12 

的JPA實體(與Hibernate作爲提供程序),它看起來像這樣:

@Column(name="REPORTING_PERIOD") 
private long reportingPeriod; 

@Temporal(TemporalType.DATE) 
@Column(name="REPORT_DATE") 
private Date reportDate; //java.util.Date 

現在,讓我們通過我的單元測試: (我使用Spring數據爲JPA庫) 下面一行REPORTING_PERIOD列查詢DB

ReportingPeriod period1 = reportingPeriodRepository.findByMaxReportingPeriod(); 
assertNotNull(period1); // works fine and entity is fetched 
System.out.println(period1.getReportDate()); 

的出放SOP的是2012-02-01 - 注意從值的自動轉換在分貝01-FEB-12

現在,如果我直接通過使用'01日期查詢 - FEB-12' ,因爲我下面做,我沒有得到任何結果:

ReportingPeriod period2 = reportingPeriodRepository.findByReportDate(period1.getReportDate()); 
assertNotNull(period2); 

注意,我使用的,我可以在前面的語句成功獲取相同的實體日期字段。

這也不工作:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
ReportingPeriod period3 = reportingPeriodRepository.findByReportDate(df.parse("2012-02-01")); 
assertNotNull(period3); 

我如何能查詢(用HQL也將確定)的REPORTING_DATE作爲帕拉姆時分貝值是01-FEB-12非常感謝任何幫助。

+0

你確定你正在使用的格式是正確的將字符串解析爲'Date'?你可以嘗試'DateFormat df = new SimpleDateFormat(「yyyy-MM-dd」);'? – 2012-04-28 18:10:29

+0

@βнɛƨнǤʋяʋиɢ - 不,我早些時候也嘗試過,但不起作用。奇怪的是,Hibernate不報告任何錯誤,只是沒有結果。 – SRK 2012-04-28 18:25:27

+0

你能不能給我們查詢我認爲錯誤/問題是在查詢或方法findByReportDate – esej 2012-04-28 18:35:47

回答

2

我覺得有一些明確的日期格式轉換而得到的結果reportingPeriodRepository.findByMaxReportingPeriod();

因此,我們可以檢查我們是否得到使用相同的格式,數據庫格式的數據

變化

DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
ReportingPeriod period3 = reportingPeriodRepository.findByReportDate(df.parse("2012-02-01")) 

DateFormat df = new SimpleDateFormat("dd-MMM-yy"); 
ReportingPeriod period3 = reportingPeriodRepository.findByReportDate(df.parse("01-FEB-12"))