Finding users whose birtday is today with JPA
Posted
by Jeduan Cornejo
on Stack Overflow
See other posts from Stack Overflow
or by Jeduan Cornejo
Published on 2010-03-31T19:15:44Z
Indexed on
2010/04/08
20:33 UTC
Read the original article
Hit count: 276
Hi, I have a table with users and are trying to get a list with the people who have birthday today so the app can send an email.
The User is defined as
@Entity
public class User {
@Size(max = 30)
@NotNull
private String name;
[...]
@Temporal(TemporalType.DATE)
@DateTimeFormat(style = "S-")
protected Date birthday;
}
and I've got a method which returns the people which were born today like so
@SuppressWarnings("unchecked")
public static List<User> findUsersWithBirthday() {
List<User> users =
entityManager().createQuery("select u from User u where u.birthday = :date")
.setParameter("date", new Date(), TemporalType.DATE)
.getResultList();
return users;
}
This is fine and all for finding people which were born today, however tha's not really that useful, so I've been struggling for a way to find all the users that were born today, independent of the year.
Using MySQL there's functions I can use like
select month(curdate()) as month, dayofmonth(curdate()) as day
However I've been struggling to find a JPA equivalent to that.
I'm using Spring 3.0.1 and Hibernate 3.3.2
© Stack Overflow or respective owner