|
From: Bijl, S. v. d. <sil...@lo...> - 2005-10-11 06:35:41
|
Hi,
=20
I've been playing around with this. I came up with the following code:
=20
=20
public class AbstractBaseHibernateDao<ObjectType, IdType extends Serializab=
le> extends
HibernateDaoSupport implements BaseDao<ObjectType, IdType> {
=20
// JDK 5.0 trickery to retrieve the object class
private Class ourClass =3D (Class)((ParameterizedType)getClass().getGeneri=
cSuperclass()).getActualTypeArguments()[0];
/*=20
* Retrieve object from db.=20
*/
@SuppressWarnings("unchecked")
public ObjectType get(IdType id) throws DataAccessException {=20=20
return (ObjectType) getHibernateTemplate().load(ourClass, id);
}
=20
}
I declared the interface similiar to your code. Hope this helps.=20
=20
Silvester
=20
________________________________
Van: spr...@li... namens Achmad Ar=
if Rachim
Verzonden: di 11-10-2005 6:33
Aan: spr...@li...; jug-indonesia@yahoogr=
oups.com
Onderwerp: [Springframework-developer] How create Generic DAO pattern in ti=
ger
How to read Inherit Generic Type class ?
Guys I've been googling all week but couldn't find solution.
Id like to create Generic DAO pattern. So here's what i do
public interface Dao<E extends Entity, I extends Serializable>{
public I create(E entity);
public E read(I id);
public void update(E entity);
public E delete(I id);
}
concrete class would be like this
public class DaoBean implements Dao{
public Serializable create(Entity entity){
getHibernateTemplate.save(entity);
return entity.getId();
}
public void update(Entity entity){
getHibernateTemplate.update(entity);
}
public Entity read(Serializable id){
return (Entity)getHibernateTemplate.load(getClass(),id);
}
public Entity delete(Serializable id){
Entity entity =3D read(id);
getHibernateTemplate.delete(entity);
return entity;
}
public Class getEntityClass(){
// now ... how to get Dao E class ????
return E.class;
}
}
The millon dollar* question is how to get Dao Generic E type class ???
(* million asked question)
Thanx=20
This e-mail and any attachment is for authorised use by the intended recipi=
ent(s) only. It may contain proprietary material, confidential information =
and/or be subject to legal privilege. It should not be copied, disclosed to=
, retained or used by, any other party. If you are not an intended recipien=
t then please promptly delete this e-mail and any attachment and all copies=
and inform the sender. Thank you.
|