如何在 JPQL 中创建 "findByDate" NamedQuery?

How to create a "findByDate" NamedQuery in JPQL?

我正在尝试为 Web 服务创建 @NamedQuery,以便可以 return 具有给定出生日期的客户列表。

GlassFish 报告以下错误:

Exception Description: Problem compiling [SELECT c FROM Customer c WHERE c.dob = to_date(:dob, 'dd-Mon-yyyy')]. [38, 45] The identification variable 'to_date' is not defined in the FROM clause.

@NamedQuery(name = "Customer.findByDob", query = "SELECT c FROM Customer c WHERE c.dob = to_date(:dob, 'dd-Mon-yyyy')")

@GET
    @Path("findByDob/{dob}")
    @Produces({"application/json"})
    public List<Customer> findByDob(@PathParam("dob") String dob) {
        Query q = em.createNamedQuery("Customer.findByDob");
        q.setParameter("dob", dob);
        return q.getResultList(); 
}

非常感谢任何反馈。

您可以在实体管理器中委托从日期到字符串数据库格式的转换。这样的事情应该有效:

@NamedQuery(name = "Customer.findByDob", query = "SELECT c FROM Customer c WHERE c.dob = :dob")

        @GET
        @Path("findByDob/{dob}")
        @Produces({"application/json"})
        public List<Customer> findByDob(@PathParam("dob") String dob) {
            SimpleDateFormat sdf = new SimpleDateFormat("dd-Mon-yyyy");
            Date dobDate = sdf.parse(dob); //Create a new Java Date object. 

            Query q = em.createNamedQuery("Customer.findByDob");
            q.setParameter("dob", dobDate); //The entity manager will transform the Date to query needs.
            return q.getResultList(); 
    }