2016-02-17 81 views
0

这是我的代码..查询工作时,我搜索与MySQL查询(找到它在屏幕截图)。但失败与Hiberbate查询。休眠查询员工与日期

EmployeeAttendanceMaster masterEmployeeFromRepository = masterEmployeeRepository.findById(employee, date); 
    if (masterEmployeeFromRepository == null) { 

     System.out.println("SignIn Successfully"); 
    }else System.out.println("You are already Logged In"); 

masterEmployeeRepository:

@Query("select me from EmployeeAttendanceMaster me where me.employee = ?1 and Date(me.date) = ?2 order by me.date desc") 
EmployeeAttendanceMaster findById(Employee employee,Date date); 

mysql db screenshot in with same query

数据与db..So同一日期还有它shoudnot到底,如果condition.It应该遵循其他condition.But只要我尝试它打印“登录成功”

感谢提前

+0

为什么你有'Date(me.date)=?2'?你尝试过'me.date =?2'吗? “EmployeeAttendanceMaster.date”列的数据类型是什么? – manish

+0

db中的日期是2016-02-17 11:58:11 ..我只想要日期..我为什么使用它在实体表中: @JsonSerialize(using = CustomLocalDateTimeSerializer.class) @JsonDeserialize(using = CustomLocalDateTimeDeserializer。类) private LocalDateTime date; –

+0

'@ Query'的完整数据类型是什么?你在使用Spring Data JPA吗? – manish

回答

0

您正在使用Spring Data JPA @Query注释(从您在问题的评论中提供的完整数据类型中辨别出)。您使用@Queryselect me from EmployeeAttendanceMaster me ...)指定的查询必须是有效的JPA查询语言(JPQL)语句。从我所知道的和记住的,JPQL没有Date()函数。所以,你的查询是无效的,因为它包含Date(me.date),它指的是一个不存在的JPQL Date()函数,即使你可以直接在MySQL上运行它。

您可以将您的查询声明更改为:

@Query(value = "select * from EmployeeAttendanceMaster where employee_id = ?1 and Date(date) = ?2 order by date desc", nativeQuery = true) 

这将迫使JPA提供商(你的情况休眠)治疗查询作为原生SQL查询,将基础数据库上没有任何执行翻译。虽然你会失去数据库独立性。

+0

请参阅上面的答案中的修改后的查询。 – manish

+0

这个查询工作就像我以前的查询..虽然员工与日期相同的数据库中有它通过“如果”条件并存储多个记录..但我想如果有相同日期的员工在数据库中它应该遵循其他条件。查询无法正常工作 –