Reputation: 4150
I am having a lot of methods of this type:
public static List<EduUsers> getDetails(Class c, Map<String, Integer> params) {
List<EduUsers> details = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(c);
for (Map.Entry<String, Integer> entry : params.entrySet()) {
cr.add(Restrictions.eq(entry.getKey(), entry.getValue()));
}
details = cr.list();
tx.commit();
} catch (Exception asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.commit();
}
} finally {
session.close();
}
return details;
}
I am trying to have a generic Method for them and this is what I have written so far:
public static <T> List<T> getDetails(Class<T> c, Class<T> b) {
List<T> details = null;
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Criteria cr = session.createCriteria(c);
//am stuck on iteration of the map from Class T b
details = cr.list();
tx.commit();
} catch (Exception asd) {
log.debug(asd.getMessage());
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return details;
}
How do I put up a generic Map onto the method?
EDIT: My Map values type can change I need to be able to put not just Integers but Strings as well and other types
Upvotes: 0
Views: 7633
Reputation: 3914
Just as easy as adding Map<? extends Object, ? extends Object>
to your method's parameters list.
public static<T> List<T> getDetails(Class<T> c, Map<? extends Object, ? extends Object> params)
Another option would be adding two other parameter types:
private static <T, S, X> List<T> getDetails(Class<T> clazz, Map<S, X> map) {
...
for (Map.Entry<S, X> entry : params.entrySet()) {
cr.add(Restrictions.eq(entry.getKey(), entry.getValue()));
}
...
}
The new parameter Class<T> b
can be removed, since it doesn't been used anywhere.
Upvotes: 1
Reputation: 2516
To use generic map you should define your method to allow a Map with any type of object to pass in Value place of Map.
Your new method should look like this getDetails(Class<T> c,Map<String,?> params)
and Map Entry iterator should look like as below:
for (Map.Entry<String, Object> entry : params.entrySet()) {
cr.add(Restrictions.eq(entry.getKey(), entry.getValue()));
}
Upvotes: 2