92 words

Calculating age in query?

#1
2012-05-29 07:20

Hi!

I've got an issue: I have a birthdate as java.sql.Date and a separate field int age. However, of course the age is not accurate because it gets set only once. So what's the best bet (JPA Standard way) to solve the issue of accessing and filtering by an age (having a birthdate) in a query?

thanks!

Alex

quasado
quasado's picture
Joined on 2011-04-30
User Post #119
#2
2012-05-30 16:57

You can use a JPQL query with a simple date comparison:

SELECT p FROM Person p WHERE p.birthDate BETWEEN :FROM AND :TO

The values for the FROM and TO parameters can be calculated just before query execution.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #1,096

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel