Results 1 to 3 of 3

Thread: Parametrized DAO problem

  1. #1
    Join Date
    Nov 2007
    Posts
    9

    Default Parametrized DAO problem

    Hi,

    I am trying to use parameterized DAO which is everyone knows from Hibernate website :
    This is the interface:

    public interface DaoGeneric<T extends MyBaseEntity, ID extends Serializable>{

    public T findById(ID id);

    public List<T> findAll();

    public void makeObjectPersistent(T entity);

    public void makeObjectTransient(T entity);

    public void flushSession();

    public void clear();

    }
    public abstract class DaoGenericHibernate<T extends MyBaseEntity,
    ID extends Serializable> extends
    HibernateDaoSupport implements DaoGeneric<T, ID> {


    private Class<T> persistentType;

    public DaoGenericHibernate() {

    this.persistentType = (Class<T>)
    ((ParameterizedType) getClass().getGenericSuperclass())
    .getActualTypeArguments()[0];
    }

    public List<T> findAll() {
    final List<T> loadedEntities = getHibernateTemplate().loadAll(getPersistentType() );
    return loadedEntities;
    }

    public T findById(Serializable id) {
    T loadedEntity = (T) getHibernateTemplate().get(getPersistentType(), id);
    return loadedEntity;
    }

    public void makeObjectPersistent(T entity) {
    getHibernateTemplate().saveOrUpdate(entity);
    }

    public void makeObjectTransient(T entity) {
    getHibernateTemplate().delete(entity);
    }

    public void flushSession() {
    getHibernateTemplate().flush();
    }

    public void clear() {
    getHibernateTemplate().clear();
    }

    public Class<T> getPersistentType() {
    return this.persistentType;
    }

    public List<T> findEntityByNamedQuery (String queryName, Object[] values) {
    List<T> result = getHibernateTemplate().findByNamedQuery(queryName, values);

    return result;
    }
    The DAO that is inheriting the Generic DAO is com.HibernateDaoImpl


    When I am trying to invoke
    the
    findById(Serializable id)


    I am getting error:
    [com.HibernateDaoImpl]: Constructor threw exception; nested exception is java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType .......
    This error occurs when

    this.persistentType = (Class<T>)
    ((ParameterizedType) getClass().getGenericSuperclass())
    .getActualTypeArguments()[0];
    is being executed.

    Any idea how to deal with this issue

    I am using Hibernate 3.2
    Spring 2.5
    And a lot of patience and persistence.

    Thanks

  2. #2
    Join Date
    Sep 2006
    Location
    Hartford, CT
    Posts
    145

    Default

    I'm not a Hibernate expert by any means, but could I suggest that if you post the code for your HibernateDaoImpl class?
    Kent Rancourt
    DevOps Engineer

  3. #3
    Join Date
    Nov 2007
    Posts
    9

    Default

    hey Kent,

    In the com.HibernateDaoImpl I have method loadSomeEntity which is delegates
    load processing to the
    findById

    here is the method:

    public SomeEntity loadSomeEntity (Long id) {

    SomeEntity someEntity = (SomeEntity ) super.findById(id);
    }
    The solution I've found is to provide as the second parameter to the loadSomeEntity () the actual class of the entity I am attempting to load.

    It works, but I am still puzzled.

    I did find some blog from Rob Harrop that migt help me, but at this point
    I have not found solution on how to find actual entity type at the moment of execution.
    thx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •