Hi Luiz,
You can't get the type of a generic type because the type information
is lost in the bytecode. The probably best solution for your problem is
to overload the method so you can either go getObject(myExample) or
getObject(MyClass.class).... to make this possible you have to make
sure you add the extends clause to the generic type (otherwise it would
be of the type Object and the complier wouldn't know which version to
pick).
Cheers,
Georg
public <T> List<T> getObject(final Class<T>
clazz) {
return getObjects(clazz);
}
public <T extends BaseObject> List<T>
getObject(final T o) {
if (o == null) {
throw new Exception("...");
} else {
HibernateCallback callback = new HibernateCallback(){
public Object doInHibernate(org.hibernate.Session
session) throws HibernateException, SQLException
{
Example ex =
Example.create(o).ignoreCase().enableLike(MatchMode.ANYWHERE);
return session.createCriteria(o.getClass()).add(ex).list();
}
};
return (List<T>)
getHibernateTemplate().execute(callback);
}
}
Luiz Fernando Rodrigues wrote:
Hi,
I was developing a generic method to get objects by example. The
problem is that I have to inform the Class object, so I can search for
all objects in case of a null parameter.
Is it possible to get the object class from the generic type T?
public <T> List<T> getObject(final T o, final
Class<T> clazz) {
if (o == null) {
return getObjects(clazz);
} else {
HibernateCallback callback = new HibernateCallback(){
public Object doInHibernate(org.hibernate.Session
session) throws HibernateException, SQLException
{
Example ex =
Example.create(o).ignoreCase().enableLike(MatchMode.ANYWHERE);
return session.createCriteria(clazz).add(ex).list();
}
};
return (List<T>)
getHibernateTemplate().execute(callback);
}
}
Luiz
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
 |
Georg Henzler
_SOFTWARE ENGINEER |
Level 1, 116 - 122 Chapel Street
Windsor Vic 3181
(Enter Via Duke Street) |
|
| T |
03 9692 6316 |
| F |
03 9692 6300 |
| M |
0437 949 328 |
| W |
sputnikagency.com |
|
|