3

Hello I am using dropwizard for my application.

The resource class

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

The DAO class

public class PersonDAO extends AbstractDAO<Person> {

public PersonDAO(SessionFactory factory) {
    super(factory);
}

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

public Person create(Person p) {
    return persist(p);

}

The Person Class

@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
@Table(name = "Person")

@NamedQueries({
    @NamedQuery( 
            name = "com.example.findAll",
            query = "SELECT distinct p.name FROM Person p"
    )
})

@JsonProperty
String name;

But When I try to access the resource it always fails saying that in the DAO class the method 'findAll' should return a List<Person> instead of List<String>. What am I missing? I checked the query with teh database and it returns the correct result. Is tehre a way to configure the return type of the query inside the namedQuery?

gevorg
  • 4,835
  • 4
  • 35
  • 52
user_mda
  • 18,148
  • 27
  • 82
  • 145

2 Answers2

0

Try removing the List generic like so:

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

It seems to cast to a List of Strings and then you can use generics further up the chain.

0

Change your list method and use a typed query

TypedQuery<String> query =
      em.createNamedQuery("com.example.findAll", String.class);
  List<String> results = query.getResultList();
mcastilloy2k
  • 478
  • 7
  • 9