简体   繁体   中英

How to calculate age with JPA 2 criteria queries?

I'm quite amazed that I could not find anything on this topic:

How do I calculate the age of a person at a given date, based on a date of birth and using only regular JPA criteria queries?

There are plenty of native SQL solutions, but I could not find a simple and elegant vendor-agnostic criteria.

Cheers!

There is no arithmetic operations for date operands in JPA 2.0. Also three existing date related functions in JP QL (CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP) and corresponding methods in Criteria API ( currentDate(), currentTime(), currentTimestamp() ) have nothing to do with calculations. That makes it impossible to perform calculations with dates. Just calculate in Java, as JB Nizet said.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM