JPA 2.0の新機能 Timestampリテラル

日付を文字列であらわせるようになった。

書式はJDBCと同じ。

つまり

{d 'yyyy-mm-dd'}
{t 'hh:mi:ss'}
{ts 'yyyy-mm-dd hh:mi:ss.fff'}

という書式。

dが日付、tが時刻、tsがタイムスタンプ(日時)をあらわします。直後にスペースがないとダメ。

    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA2TestPU");
        EntityManager em = emf.createEntityManager();
        String jpql = "select e " +
                "from Employee e " +
                "where e.entdate < {d '2000-06-01'}";

        List<Employee> result = em.createQuery(jpql).getResultList();
        for(Employee e : result){
            System.out.printf("%d:%s - %tF%n" ,e.getId() ,e.getName() , e.getEntdate());
        }

        em.close();
        emf.close();

    }

所詮リテラルだけなので使い道は正直ないと思う。パラメータはCalendarかDateじゃないとダメだし。

文字列の連結でパラメータを渡すなんてないでしょう?…たぶん