2012-04-28 118 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"))