Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:47:21+00:00 2026-06-04T12:47:21+00:00

I have previously only used reflection to do things like dynamically get class and

  • 0

I have previously only used reflection to do things like dynamically get class and set field values in it. My Google search showed me that I could also possibly use reflection for dynamic type casting?

My code is as follows:

import entity.Shipvia;
import entity.Route;
import java.lang.reflect.Field;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class RetrieveResultList {

    public static List retrieveResultList(String tablename) {
        EntityManager entityManager = Persistence.createEntityManagerFactory("EntityLibraryPU").createEntityManager();
        Query query = entityManager.createNamedQuery(tablename + ".findAll");
        List<Shipvia> resultList = query.getResultList();
        return resultList;
    }
}

I am using this method to dynamically retrieve result from a database table. Because the table name is always different, I cannot have List as it will be different for each table.

How would I go about converting the tablename string that I am passing in, to be the type of the List?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:47:23+00:00Added an answer on June 4, 2026 at 12:47 pm

    You can’t do that and even if you could, it would be useless as all generics information is removed from the Java code when it’s compiled, only casts would be there and as you would be using reflection there would be no casts to be made.

    The closest thing you will be able to do is, instead of sending in a String send a Class object. The caller code would have to say which class it wants (the caller probably knows what kind of object it’s using) and you would use it to make the list have the correct generic.

    A very simple implementation would be something like this:

    List<Shipvia> shipvias = RetrieveResultList.retrieveResultList( Shipvia.class );
    

    And implementation could be something like this:

    public class RetrieveResultList {
    
        private static final EntityManagerFactory FACTORY = Persistence.createEntityManagerFactory("EntityLibraryPU");
    
        public static <T> List<T> retrieveResultList(Class<T> type) {
            EntityManager entityManager = FACTORY.createEntityManager();
            String tablename = type.getName(); // figure out table name from **type**
            Query query = entityManager.createNamedQuery(tablename + ".findAll");
            List<T> resultList = query.getResultList();
            entityManager.close();
            return resultList;
        }
    }
    

    And then you should have something like what you’re looking for.

    ALSO, DO NOT create an entity manager factory on every call to this method, the entity manager factory MUST BE a singleton in your project as it’s a very expensive object to create. You should also close the EntityManager you created before leaving the method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Previously i have used restful wcf webservices to get the data from server.But now
Previously I have only used the Scala templates, but now am I using Java
I have an HTML form that previously was only used for text attributes for
Previously, I have a Google App Engine application ( jstock-static ), which is only
Suppose that we have previously instantiated three objects A, B, C from class D
I'm new to Selenium RC, having previously used Selenium IDE and only run tests
I have used Spring before but I am no expert and only used it
I have previously used XML based configuration for my Spring app. Now, I want
I only noticed this last night, as I have not used the toolbox on
I have previously used has_and_belongs_to_many associations in my older Rails apps but am moving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.