java - How to specify a Generic Type as function parameter -


i want create jpa transaction jse app returns result.

@suppresswarnings("unchecked") public static <t> t transaction(emworker worker, class<t>clazz){      entitymanager em = createentitymanager();     em.gettransaction().begin();      //  >>> functional interface     object result = worker.work(em);      em.gettransaction().commit();     em.close();     return (t)result; } 

it works fetching single object :

funkopop gandalf=emfactory.transaction(   emanager -> {      return emanager.find(funkopop.class, 1); }, funkopop.class); 

but now, want list of funkopop.

list<funkopop> list =emfactory.transaction(   e -> {      string query = "select f funkopop f ";     list<funkopop> l = e.createquery(query, funkopop.class).getresultlist();     return l;  },  list<funkopop>.class);  //won't compile ; or list.class gives warnings 

the transaction needs 2 arguments : lambda , class. approximatively understand cannot capture type list<funkopop>.class parameter loose <funkopop> generic. solution have no warnings ?

you can't because there no list<funkopop> type. there list.

you may hard way:

list<t> transaction(emworker worker, class<t>clazz) {   ...   list<t> l = (list<t>)e.createquery(query, clazz).getresultlist();   return l; } 

you might need rename method.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -