views:

47

answers:

1

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

+1  A: 

I don't know how to do that in JPQL but HQL has day() and month() functions so you could run:

from User u 
where day(u.birthday) = day(CURRENT_DATE) 
and month(u.birthday) = month(CURRENT_DATE) 

If using HQL is not an option, I would go for a native query.

Pascal Thivent