I have the following MySQL query:
select p.name, sum(timestampdiff(MINUTE, r.start_time, r.end_time))
from ride r inner join person p on r.driver_id=p.id
group by p.id
I want to write this in a JPA Query. The code below throws a NullPointerException, it does not work:
@RestResource
public interface SomeRepository extends CrudRepository<Ride, Long> {
@Query("SELECT new com.sample.dto.MyDTO(d.name, SUM(FUNCTION('TIMESTAMPDIFF', 'MINUTE', r.startTime, r.endTime))) " +
"FROM Ride r JOIN r.driver d WHERE r.startTime BETWEEN ?1 AND ?2 " +
"GROUP BY d.id" )
List<MyDTO> findSomething(@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime);
}
If I use DATEDIFF function, it works.
FUNCTION('DATEDIFF', r.startTime, r.endTime)
But I need the exact difference in minutes. TIMESTAMPDIFF satisfies my needs. After failure of finding how to do this with JPA, I returned to native query but this is not good. Any alternatives?
I use the latest spring-data-jpa (2.1.3), hibernate-core:5.3.7
public class Ride {
@Column(name = "start_time")
private LocalDateTime startTime;
@Column(name = "end_time")
private LocalDateTime endTime;
}
The goal is to find total difference between startTime and endTime (in minutes). Is there a way to do this with standard (portable) JPA?