Annoying Unchecked Conversion using JPA
I’m a great user of the Java Persistence API (JPA). It just simplifies the code a lot when interacting with databases. Even with the simplification, the code keeps its readability because the API is object-oriented, very close to our mental abstraction. However, it is still very criticized because of pending features like Criteria, a better default implementation (Toplink sucks!) and a consistent implementation considering Generics. Actually, the last one is the reason of this post, which describes an insignificant but annoying experience with unchecked conversion caused by a weak support of Generics.
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 List<Transcription> findTranscriptions(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<transcription>
. To stop the warning, we are obliged to add the annotation @SuppressWarnings(“unchecked”)
just before the method declaration as shown below.
@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.
Recent Posts
Clojure Books in the Toronto Public Library

Knowledge Portfolio 2023

Once Upon a Time in Russia

FHIR: A Standard For Healthcare Data Interoperability

First Release of CSVSource

Never Too Soon to Change Plans

Bootstrapping my Repositories

Reaching RMM Level 1 in My Repositories

The Repositories of my Portfolio

Knowledge Portfolio 2022

Astonishing Carl Sagan's Predictions Published in 1995

Making a Configurable Go App

Dealing With Pressure Outside of the Workplace

Reacting to File Changes Using the Observer Design Pattern in Go

Provisioning Azure Functions Using Terraform
