以下查询会引发错误
@GET
@Path("findByStartEndVari/{startdate}/{enddate} ")
@Produces({"application/json"})
public List<Dailyrecords> findByStartEndVari(@PathParam("startdate") Date startdate, @PathParam("enddate") Date enddate)
{
TypedQuery<Dailyrecords> q = em.createQuery("SELECT d.painlevel, d.painlocation FROM Dailyrecords d WHERE d.submitdate BETWEEN :startdate AND :enddate ", Dailyrecords.class);
q.setParameter("startdate", startdate);
q.setParameter("enddate", enddate);
return q.getResultList();
}
答案 0 :(得分:0)
您当前的语法不正确。如果您希望JPQL查询返回DailyRecords
对象,则需要选择整个对象:
TypedQuery<Dailyrecords> q = em.createQuery("SELECT d FROM Dailyrecords d WHERE d.submitdate BETWEEN :startdate AND :enddate ", Dailyrecords.class);
q.setParameter("startdate", startdate);
q.setParameter("enddate", enddate);
如果您确实只想要指定列,则可以尝试以下操作:
Query query = session.createQuery("SELECT d.painlevel, d.painlocation FROM Dailyrecords d WHERE d.submitdate BETWEEN :startdate AND :enddate");
List<Object[]> rows = query.list();
当您查询单个列时,您将返回Object[]
,数组中的每个条目对应一列。所以在这种情况下,第一个条目是疼痛水平,第二个条目是疼痛位置。