Java - Dropwizard + Hibernate - compiler error when using namedQuery

Viewed 254

I'm following Dropwizard's guide on integrating Hibernate, here's a modified code snippet referring to the DAO based on their example and other tutorials:

public class SayingDAO extends AbstractDAO<Saying> {
    public SayingDAO(SessionFactory factory) {
        super(factory);
    }

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

However, #findAll is both highlighted by my IDE (IntelliJ IDEA), with the following:

Cannot resolve method 'list(org.hibernate.query.Query<capture<?>>)'

And causes a compilation error when running mvn package:

[28,16] no suitable method found for list(org.hibernate.query.Query<capture#1 of ?>)
[ERROR]     method io.dropwizard.hibernate.AbstractDAO.list(org.hibernate.Criteria) is not applicable
[ERROR]       (argument mismatch; org.hibernate.query.Query<capture#2 of ?> cannot be converted to org.hibernate.Criteria)
[ERROR]     method io.dropwizard.hibernate.AbstractDAO.list(javax.persistence.criteria.CriteriaQuery<com.example.api.Saying>) is not applicable
[ERROR]       (argument mismatch; org.hibernate.query.Query<capture#2 of ?> cannot be converted to javax.persistence.criteria.CriteriaQuery<com.example.api.Saying>)
[ERROR]     method io.dropwizard.hibernate.AbstractDAO.list(org.hibernate.query.Query<com.example.api.Saying>) is not applicable
[ERROR]       (argument mismatch; org.hibernate.query.Query<capture#2 of ?> cannot be converted to org.hibernate.query.Query<com.example.api.Saying>)

Changing it to the following solves the compiler error:

public List<Saying> findAll() {
        return (List<Saying>) namedQuery("com.example.api.Saying.findAll").getResultList();
    }

But still shows an IDE warning (not an error):

Unchecked cast: 'java.util.List<capture<?>>' to 'java.util.List<com.example.api.Saying>'

I'm relatively new to Java; am I missing something or is Dropwizard's guide wrong here?

0 Answers
Related