lundi 14 mars 2016

Abstracting a Method that Returns a List of Objects

I am working on a reporting module. Each report runs at least one more unique database queries to collect the report data.

Even though the report queries are unique the methodology of calling them is identical.

I am trying to abstract the method to return a List so I can call my unique queries through a common method, instead of having a separate method for each object I am trying to get results for.

The method will look something like this:

public List<?> getQueryResults(EntityManager em, String query, 
List<String> parameters, Class clazz) {
   TypedQuery<?> query = em.createQuery(query, clazz);

   // set parameters here...

   List<?> results = query.getResultList();

   return results;
}

The above flow is pretty much repeated for all queries. The parameters that have to be set are three different configurations usually so I can handle that separately.

What I am trying to figure out is how do I pass to the method or somehow derive the class that I need to used for TypedQuery<?> and List<?>.

I was hoping that I could do this TypedQuery<clazz> but that does not work. Should I use reflection and derive it from the clazz argument?





Aucun commentaire:

Enregistrer un commentaire