Search code examples
jpaeclipselinkjpql

JPQL - no date arithmetic


I understand JPQL does not let us do date arithmetic, but i'm new to it so i'm wondering if there's a simple way to tackle this problem (i'm using eclipselink):

I have two entities, entity A which has a one-to-many relationship with entity B.

Entity A stores the time, and each record for entity B has a date.

I want to select rows from entity B between a date range, taking into account the time field of entity A which it belongs to.

One solution i've thought about is just fetching all entities within the date range (ignoring the time), and then manually filtering out the results taking into account the time in entity A, after the query has ran.

But, I want to know if there's a JPA way, which won't result in unnecessary records being fetched.


Solution

  • EclipseLink supports a FUNC keyword in JPQL that allows calling any database function.

    You database will provide date arithmetic functions, so you can use these.

    See, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Support_for_Native_Database_Functions