The code below shows a business method in a stateless session beans that returns a list of Transcription's instances created by the JPA with data retrieved from the database.
public ListfindTranscriptions(Segment segment) { Query query em.createQuery("select t from Transcription t where t.segment = :segment"); query.setParameter("segment", segment); return query.getResultList(); }
When we implement this method using Netbeans, no warning message appears automatically. It might not occur because the editor doesn't understand the word "Transcription" in the query as an entity class. However, the warning message below appears if we put the additional parameter -Xlint:unchecked, which tells the compiler to give more detail for unchecked conversion warnings.
<...>/AnnotationServiceBean.java:70: warning: [unchecked] unchecked conversion found : java.util.List required: java.util.List<...entity.Transcription> .getResultList(); 1 warning
According to this document, created by Sean Brydon at Sun Microsystems, the warning is generated because query.getResultList() returns a non-generic version of List, when the return type expects "List
@SuppressWarnings("unchecked")
public List findTranscriptions(Segment segment) {
Query query em.createQuery("select t from Transcription t
where t.segment = :segment");
query.setParameter("segment", segment);
return query.getResultList();
}
The same document mentions that in a future version of the Java Persistence API, the javax.persistence.Query class will likely change to better support generics. This post is to stimulate them to do it faster, because for me @SuppressWarnings("unchecked") is actually a dirt in my beautiful and legible source code.
0 comments:
Post a Comment