JPA .. erro no DAO Genercio ao usar metodo FIND ..21::Can not find by Class

3 respostas
P

Olá ,

tento usar o metodo abaixo passando o objeto mas me dá erro
como faço pra usar esse metodo ???

/**
	 * @see BaseDao#find(ID) BaseDao.find(T entity)
	 */
    public T find(T entity) throws DaoException {
    	T t = null;
    	try {
    		t = conn.getManager().find(domainClass, entity);
    		return t;
    	} catch(Exception e) {
    		throw new Exception(e);
    	}
    }


erro que  :

21::Can not find by Class

como tento acessar esse metodo

try{
			
			
			Co co = new Co();
			
                  co.setID(10);
                  // vou tentar buscar objeto pela PK a ideia não seria colocar qq campo do objeto e ele achar

			dao.find(co);


		} catch (Exception e) {
			System.out.println("Erro   ="+e);
			e.printStackTrace();
		}

3 Respostas

GraveDigger

O método find recebe classe e valor da PK como parâmetros, e não o objeto em si.

Curiosidade: Qual o tipo da sua variável “conn” ?

[]'s

P

caro

eu tenho o seguinte DAOGENERICO abaixo e tenho esse metodo ;;;

public T find(T entity) throws DaoException {
    	T t = null;
    	try {
    		t = conn.getManager().find(domainClass, entity);
    		return t;
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_FIND_BY_CLASS), "Can not find by Class", e);
    	}
    }
public abstract class AbstractBaseDao<T extends Entity, ID extends Serializable> extends JpaDaoSupport implements BaseDao<T, ID> {
	protected final static Long METHOD_FIND_BY_ID 		= 0L;
	protected final static Long METHOD_FIND_BY_CLASS 	= 1L;
	protected final static Long METHOD_PERSIST 			= 2L;
	protected final static Long METHOD_MERGE 			= 3L;
	protected final static Long METHOD_REMOVE 			= 4L;
	protected final static Long METHOD_REMOVE_BY_ID 	= 5L;
	protected final static Long METHOD_GET_LIST 		= 6L;
	protected final static Long METHOD_COUNT 			= 7L;
	
	protected Conexao conn= null;

	public AbstractBaseDao() {
		imsDBMng = DBMng.getInstance();
	}

	/* Classe que representa o objeto de domínio gerenciado pelo DAO. */
    protected Class<T> domainClass = getDomainClass();

    /* Retorna a classe do objeto de domínio gerenciado pelo DAO. */
    @SuppressWarnings("unchecked")
	protected Class<T> getDomainClass() {
        if (domainClass == null) {
        	ParameterizedType thisType = (ParameterizedType) getClass().getGenericSuperclass();
            domainClass = (Class<T>) thisType.getActualTypeArguments()[0];
        }
        return domainClass;
    }

	/**
	 * @see BaseDao#find(ID) BaseDao.find(ID id)
	 */
    public T find(ID id) throws DaoException {
    	T t = null;
    	try {
    		t = conn.getManager().find(domainClass, id);
    		return t;
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_FIND_BY_ID), "Can not find by ID", e);
    	}
    }

	/**
	 * @see BaseDao#find(ID) BaseDao.find(T entity)
	 */
    public T find(T entity) throws DaoException {
    	T t = null;
    	try {
    		t = conn.getManager().find(domainClass, entity);
    		return t;
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_FIND_BY_CLASS), "Can not find by Class", e);
    	}
    }

	/**
	 * @see BaseDao#persist(ID) BaseDao.persist(T t)
	 */
    public void persist(T t) throws DaoException {
    	try {
    		imsDBMng.getManager().persist(t);
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_PERSIST), "Can not insert", e);
    	}
    }
	/**
	 * @see BaseDao#remove(ID) BaseDao.remove(T t)
	 */
    public void remove(T t) throws DaoException {
    	try {
    		imsDBMng.getManager().remove(t);
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_REMOVE), "Can not remove", e);
    	}
    }

	/**
	 * @see BaseDao#remove(ID) BaseDao.merge(T t)
	 */
    public void merge(T t) throws DaoException {
    	try {
    		imsDBMng.getManager().merge(t);
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_MERGE), "Can not update", e);
    	}
    }

    
	/**
	 * @see BaseDao#getList() List<T> BaseDao.getList()
	 */
    @SuppressWarnings("unchecked")
    public List<T> getList() throws DaoException {
       	List<T> l_list = new ArrayList<T>();
        try {              
        	l_list = this.imsDBMng.getManager().createQuery("select x from " + domainClass.getName() + " x").getResultList();
        	
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_GET_LIST), "Can not get the List of", e);
    	}
        return l_list;
    }

	/**
	 * @see BaseDao#removeById(ID) BaseDao.removeById(ID id)
	 */
    public void removeById(ID id) throws DaoException {
    	try {
	        Object obj = find(id);
	        conn.getManager().remove(obj);
    	} catch(Exception e) {
    		throw new DaoException(getErrorCode(METHOD_REMOVE_BY_ID), "Can not remove by ID", e);
    	}
    }

	/**
	 * @see BaseDao#count() BaseDao.count()
	 */
    @SuppressWarnings("unchecked")
	public int count() throws DaoException {
    	Long resultado = new Long(0L);
    	Query query =conn.getManager().createQuery("select count(e) from " + domainClass.getName()+" e" );
    	resultado = (Long) query.getSingleResult();
    	return resultado.intValue();
    }

}
GraveDigger

Qual erro vc está obtendo ?
Coloca seu StackTrace aqui pra gente avaliar melhor

Criado 29 de dezembro de 2008
Ultima resposta 30 de dez. de 2008
Respostas 3
Participantes 2