try{Coco=newCo();co.setID(10);// vou tentar buscar objeto pela PK a ideia não seria colocar qq campo do objeto e ele achardao.find(co);}catch(Exceptione){System.out.println("Erro ="+e);e.printStackTrace();}
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
paribe
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);
}
}
publicabstractclassAbstractBaseDao<TextendsEntity,IDextendsSerializable>extendsJpaDaoSupportimplementsBaseDao<T,ID>{protectedfinalstaticLongMETHOD_FIND_BY_ID=0L;protectedfinalstaticLongMETHOD_FIND_BY_CLASS=1L;protectedfinalstaticLongMETHOD_PERSIST=2L;protectedfinalstaticLongMETHOD_MERGE=3L;protectedfinalstaticLongMETHOD_REMOVE=4L;protectedfinalstaticLongMETHOD_REMOVE_BY_ID=5L;protectedfinalstaticLongMETHOD_GET_LIST=6L;protectedfinalstaticLongMETHOD_COUNT=7L;protectedConexaoconn=null;publicAbstractBaseDao(){imsDBMng=DBMng.getInstance();}/* Classe que representa o objeto de domínio gerenciado pelo DAO. */protectedClass<T>domainClass=getDomainClass();/* Retorna a classe do objeto de domínio gerenciado pelo DAO. */@SuppressWarnings("unchecked")protectedClass<T>getDomainClass(){if(domainClass==null){ParameterizedTypethisType=(ParameterizedType)getClass().getGenericSuperclass();domainClass=(Class<T>)thisType.getActualTypeArguments()[0];}returndomainClass;}/** * @see BaseDao#find(ID) BaseDao.find(ID id) */publicTfind(IDid)throwsDaoException{Tt=null;try{t=conn.getManager().find(domainClass,id);returnt;}catch(Exceptione){thrownewDaoException(getErrorCode(METHOD_FIND_BY_ID),"Can not find by ID",e);}}/** * @see BaseDao#find(ID) BaseDao.find(T entity) */publicTfind(Tentity)throwsDaoException{Tt=null;try{t=conn.getManager().find(domainClass,entity);returnt;}catch(Exceptione){thrownewDaoException(getErrorCode(METHOD_FIND_BY_CLASS),"Can not find by Class",e);}}/** * @see BaseDao#persist(ID) BaseDao.persist(T t) */publicvoidpersist(Tt)throwsDaoException{try{imsDBMng.getManager().persist(t);}catch(Exceptione){thrownewDaoException(getErrorCode(METHOD_PERSIST),"Can not insert",e);}}/** * @see BaseDao#remove(ID) BaseDao.remove(T t) */publicvoidremove(Tt)throwsDaoException{try{imsDBMng.getManager().remove(t);}catch(Exceptione){thrownewDaoException(getErrorCode(METHOD_REMOVE),"Can not remove",e);}}/** * @see BaseDao#remove(ID) BaseDao.merge(T t) */publicvoidmerge(Tt)throwsDaoException{try{imsDBMng.getManager().merge(t);}catch(Exceptione){thrownewDaoException(getErrorCode(METHOD_MERGE),"Can not update",e);}}/** * @see BaseDao#getList() List<T> BaseDao.getList() */@SuppressWarnings("unchecked")publicList<T>getList()throwsDaoException{List<T>l_list=newArrayList<T>();try{l_list=this.imsDBMng.getManager().createQuery("select x from "+domainClass.getName()+" x").getResultList();}catch(Exceptione){thrownewDaoException(getErrorCode(METHOD_GET_LIST),"Can not get the List of",e);}returnl_list;}/** * @see BaseDao#removeById(ID) BaseDao.removeById(ID id) */publicvoidremoveById(IDid)throwsDaoException{try{Objectobj=find(id);conn.getManager().remove(obj);}catch(Exceptione){thrownewDaoException(getErrorCode(METHOD_REMOVE_BY_ID),"Can not remove by ID",e);}}/** * @see BaseDao#count() BaseDao.count() */@SuppressWarnings("unchecked")publicintcount()throwsDaoException{Longresultado=newLong(0L);Queryquery=conn.getManager().createQuery("select count(e) from "+domainClass.getName()+" e");resultado=(Long)query.getSingleResult();returnresultado.intValue();}}
GraveDigger
Qual erro vc está obtendo ?
Coloca seu StackTrace aqui pra gente avaliar melhor