2010-03-31 44 views
2

的用戶我有一個表與用戶,並試圖獲得今天有生日的人的名單,以便應用程序可以發送電子郵件。尋找今日生日與JPA

用戶被定義爲

@Entity 
public class User { 

    @Size(max = 30) 
    @NotNull 
    private String name; 

    [...] 
    @Temporal(TemporalType.DATE) 
    @DateTimeFormat(style = "S-") 
    protected Date birthday; 
} 

和我有它返回了今天出生的,像這樣

@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; 
} 

這是罰款和所有尋找這是人的人的方法今天出生,但是這並不是那麼有用,所以我一直在努力尋找所有今天獨立於今年出生的用戶。

使用MySQL有功能我可以像使用

select month(curdate()) as month, dayofmonth(curdate()) as day 

但是我一直在努力尋找一種JPA相當於。

我使用Spring 3.0.1和3.3.2的Hibernate

回答

2

我不知道該怎麼做,在JPQL但HQL具有day()month()功能,因此你可以運行:

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

如果使用HQL不是一個選項,我會選擇一個本地查詢。