String 返回列表的命名查询<;字符串>;

String 返回列表的命名查询<;字符串>;,string,jpa,dropwizard,named-query,String,Jpa,Dropwizard,Named Query,您好,我正在为我的应用程序使用dropwizard 资源类 @Path("/people") @Produces(MediaType.APPLICATION_JSON) public class PeopleResource{ private PersonDAO pdao; public PeopleResource(PersonDAO pdao) { this.pdao = pdao; } @GET @Timed @Unit

您好,我正在为我的应用程序使用dropwizard

资源类

@Path("/people")
@Produces(MediaType.APPLICATION_JSON)
public class PeopleResource{

    private PersonDAO pdao;

    public PeopleResource(PersonDAO pdao) {
        this.pdao = pdao;
    }

    @GET
    @Timed
    @UnitOfWork
    public List<String> getAllPeople() {
        return pdao.findAll();
    }
}

但是当我尝试访问资源时,它总是失败,因为在DAO类中,方法“findAll”应该返回
List
,而不是
List
。我错过了什么?我用数据库检查了查询,它返回了正确的结果。tehre是在namedQuery中配置查询返回类型的一种方法吗?

尝试删除列表,如下所示:

public List findAll() {
  return list(namedQuery("com.example.findAll"));
}

它似乎转换为字符串列表,然后您可以在链的更上层使用泛型。

更改列表方法并使用类型化查询

TypedQuery<String> query =
      em.createNamedQuery("com.example.findAll", String.class);
  List<String> results = query.getResultList();
TypedQuery查询=
em.createNamedQuery(“com.example.findAll”,String.class);
List results=query.getResultList();
public List findAll() {
  return list(namedQuery("com.example.findAll"));
}
TypedQuery<String> query =
      em.createNamedQuery("com.example.findAll", String.class);
  List<String> results = query.getResultList();