5

I do a lot of querying with javax.persistence. And Java forces me to either suffer warnings or @Suppress them, neither of which feels right.

@SuppressWarnings("unchecked")
@Override
public List<Area> getArea(Province province) {
    Query qry = em.createQuery("Select c from Area c where c.province = ?1");
    qry.setParameter(1, province);
    return qry.getResultList();
}

How do I eliminate the warning in the above code?

Perception
  • 79,279
  • 19
  • 185
  • 195
Johan
  • 74,508
  • 24
  • 191
  • 319

2 Answers2

10

If you're using JPA annotations, use TypedQuery instead of Query:

TypedQuery<Area> qry = em.createQuery(
    "Select c from Area c where c.province = ?1", Area.class);
Perception
  • 79,279
  • 19
  • 185
  • 195
Igor Rodriguez
  • 1,196
  • 11
  • 16
1

You can use the Criteria API to avoid this. Check out this question for info on how to use it.

Community
  • 1
  • 1
Barend
  • 17,296
  • 2
  • 61
  • 80
  • +1, however in this case it is not necessary to use the Criteria API, but the code of the OP can be ported to TypedQuery that uses generics and requires less rework. – gd1 Feb 24 '13 at 12:08
  • The convoluted way in which the Criteria API forces you to writes queries makes my head hurt. It's an exercise in masochism. – Johan Feb 24 '13 at 12:10