Java tutorial
// license-header java merge-point /** * Attention: Generated source! Do not modify by hand! * Generated by: HibernateEntityFactory.vsl in andromda-hibernate-cartridge. */ package br.mdarte.exemplo.academico.cd; import org.hibernate.HibernateException; import org.hibernate.ObjectNotFoundException; import org.hibernate.Session; import org.hibernate.criterion.Example; import org.hibernate.criterion.MatchMode; import org.hibernate.Criteria; import org.hibernate.CallbackException; import org.hibernate.CacheMode; import java.util.List; import java.util.Iterator; import java.util.ArrayList; import org.apache.commons.beanutils.PropertyUtils; import br.mdarte.exemplo.academico.to.EstudanteTOImpl; import br.mdarte.exemplo.academico.cd.AbstractDAO; import br.mdarte.exemplo.academico.cd.DAOException; import br.mdarte.exemplo.academico.cd.AbstractEntity; import br.ufrj.coppetec.ValueObject; import br.ufrj.coppetec.DataObject; import br.ufrj.coppetec.to.AbstractTO; import br.mdarte.exemplo.academico.util.PaginationStrategy; import br.mdarte.exemplo.academico.util.NoPagination; import br.mdarte.exemplo.academico.util.Constantes; import org.dom4j.Document; import org.dom4j.Element; /** * <p> * Factory class. * Is able to find and create objects of type Estudante. * The Hibernate <em>subclass</em> inheritance * strategy is followed. * Those can be described as follows: * </p> * @see br.mdarte.exemplo.academico.cd.Estudante */ public abstract class EstudanteDAO extends AbstractDAO { protected void removeRelations(AbstractEntity obj) throws DAOException { try { EstudanteAbstract entidade = (EstudanteAbstract) obj; Session session = currentSession(); entidade.setCursoInverse(null); } catch (HibernateException e) { throw new DAOException(e); } } public List<AbstractEntity> select(long id) throws DAOException { List<AbstractEntity> lista = new ArrayList<AbstractEntity>(); try { Session session = currentSession(); EstudanteAbstract res = (EstudanteAbstract) session.load(EstudanteImpl.class, new Long(id)); org.hibernate.Hibernate.initialize(res); lista.add(res); } catch (ObjectNotFoundException onfe) { throw new DAOException(onfe); } catch (HibernateException he1) { throw new DAOException(he1); } return lista; } public List<AbstractEntity> insert(AbstractEntity obj) throws DAOException { List<AbstractEntity> lista = new ArrayList<AbstractEntity>(); try { Session session = currentSession(); session.setCacheMode(CacheMode.GET); session.save(obj); session.flush(); lista.add(obj); return lista; } catch (HibernateException e) { throw new DAOException(e); } } public List<AbstractEntity> insertOrUpdate(AbstractEntity obj) throws DAOException { List<AbstractEntity> lista = new ArrayList<AbstractEntity>(); try { Session session = currentSession(); session.setCacheMode(CacheMode.GET); session.saveOrUpdate(obj); session.flush(); lista.add(obj); return lista; } catch (HibernateException e) { throw new DAOException(e); } } public List<AbstractEntity> update(AbstractEntity obj) throws DAOException { List<AbstractEntity> lista = new ArrayList<AbstractEntity>(); try { Session session = currentSession(); session.setCacheMode(CacheMode.GET); session.update(obj); session.flush(); lista.add(obj); return lista; } catch (HibernateException e) { throw new DAOException(e); } } public List<AbstractEntity> delete(AbstractEntity obj) throws DAOException { try { Session session = currentSession(); removeRelations(obj); session.delete(obj); session.flush(); return null; } catch (HibernateException e) { throw new DAOException(e); } } /** * Lista todos os objetos. * * @return Lista de objetos */ public List<AbstractEntity> list(PaginationStrategy paginacao, String propriedade, Boolean desc) throws DAOException { try { Session session = currentSession(); String hql = "from br.mdarte.exemplo.academico.cd.Estudante"; if (propriedade != null && !propriedade.equals("")) { String order = desc.booleanValue() ? " desc" : " asc"; hql += " order by " + propriedade + order; } org.hibernate.Query qry = session.createQuery(hql); if (paginacao == null) paginacao = new NoPagination(); paginacao.paginateResult(qry); qry.setCacheable(true); return qry.list(); } catch (HibernateException h) { throw new DAOException(h); } } public List<AbstractEntity> list() throws DAOException { try { Session session = currentSession(); org.hibernate.Query qry = session.createQuery("from br.mdarte.exemplo.academico.cd.Estudante"); qry.setCacheable(true); List res = qry.list(); return res; } catch (HibernateException h) { throw new DAOException(h); } } protected abstract Object handleFilter(DataObject vo) throws DAOException; public List<AbstractEntity> defaultFilter(AbstractTO to) throws DAOException { return defaultFilter(to, new NoPagination()); } public List<AbstractEntity> defaultFilter(AbstractTO to, PaginationStrategy paginacao) throws DAOException { Session session = currentSession(); Criteria criterios = ((org.hibernate.Session) session).createCriteria(EstudanteImpl.class); if (to instanceof EstudanteTOImpl) { EstudanteAbstract entityAbstract; try { entityAbstract = EstudanteAbstract.getEntityFromTO((EstudanteTOImpl) to); criterios.add(Example.create(entityAbstract).enableLike(MatchMode.ANYWHERE)); } catch (Exception e) { e.printStackTrace(); return null; } } else { return null; } try { if (paginacao == null) paginacao = new NoPagination(); paginacao.paginateResult(criterios); criterios.setCacheable(true); return criterios.list(); } catch (HibernateException h) { throw new DAOException(h); } } public List<AbstractEntity> filter(DataObject vo) throws DAOException { return filter(vo, new NoPagination()); } public List<AbstractEntity> filter(DataObject vo, PaginationStrategy paginacao) throws DAOException { try { Object obj = handleFilter(vo); if (paginacao == null) paginacao = new NoPagination(); if (obj instanceof org.hibernate.Query) { org.hibernate.Query res = (org.hibernate.Query) obj; paginacao.paginateResult(res); res.setCacheable(true); return res.list(); } else if (obj instanceof org.hibernate.Criteria) { org.hibernate.Criteria res = (org.hibernate.Criteria) obj; paginacao.paginateResult(res); res.setCacheable(true); return res.list(); } else return null; } catch (HibernateException h) { throw new DAOException(h); } } protected abstract org.hibernate.Criteria handleXmlExport(ValueObject vo, org.hibernate.Session session) throws DAOException; public org.hibernate.Criteria xmlExport(ValueObject vo, org.hibernate.Session session) throws DAOException { return handleXmlExport(vo, session); } public int xmlImportEntity(org.dom4j.Element raiz) throws DAOException { Session session = currentSession(); org.hibernate.Criteria critBusca_Entidade = session.createCriteria(EstudanteImpl.class); java.util.List listaRes = critBusca_Entidade.setMaxResults(2).list(); if (listaRes.size() > 1) throw new DAOException("error.default.exportid.notunique.exception" + "," + listaRes.get(0).toString() + "," + listaRes.get(1).toString()); EstudanteImpl entidade; if (listaRes.size() == 0) entidade = new EstudanteImpl(); else entidade = (EstudanteImpl) listaRes.get(0); if (raiz.selectNodes("./matricula").size() != 0) { java.lang.String valormatricula = java.lang.String.valueOf(raiz.valueOf("./matricula")); entidade.setMatricula(valormatricula); } if (raiz.selectNodes("./nome").size() != 0) { java.lang.String valornome = java.lang.String.valueOf(raiz.valueOf("./nome")); entidade.setNome(valornome); } Element raiz_curso = (Element) raiz.selectSingleNode("./curso"); if (raiz_curso.elements().size() != 0) { org.hibernate.Criteria critBusca_curso = session .createCriteria(br.mdarte.exemplo.academico.cd.CursoImpl.class); java.util.List listaResAss_curso = critBusca_curso.setMaxResults(2).list(); if (listaResAss_curso.size() > 1) // Encontrou mais de uma entidade para o mesmo cdigo throw new DAOException("error.default.exportid.notunique.exception" + "," + listaResAss_curso.get(0).toString() + "," + listaResAss_curso.get(1).toString()); if (listaResAss_curso.size() == 0) // Nao encontrou entidade associada return 1; if (listaResAss_curso.size() == 1) { br.mdarte.exemplo.academico.cd.CursoImpl entidade_ass_curso; entidade_ass_curso = (br.mdarte.exemplo.academico.cd.CursoImpl) listaResAss_curso.get(0); entidade.setCurso(entidade_ass_curso); } } else { entidade.setCurso(null); } insertOrUpdate(entidade); return 0; } public Element xmlExportEntity(AbstractEntity entity) throws DAOException { Element raiz = org.dom4j.DocumentHelper.createElement("raiz_elemento"); return xmlExportEntity(entity, raiz); } public Element xmlExportEntity(AbstractEntity entity, Element raiz) throws DAOException { try { EstudanteImpl obj = (EstudanteImpl) entity; if (obj.getMatricula() != null) { Element attmatricula = raiz.addElement("matricula"); attmatricula.addText(obj.getMatricula().toString()); } if (obj.getNome() != null) { Element attnome = raiz.addElement("nome"); attnome.addText(obj.getNome().toString()); } br.mdarte.exemplo.academico.cd.CursoImpl assEntitycurso = (br.mdarte.exemplo.academico.cd.CursoImpl) obj .getCurso(); Element asscurso = raiz.addElement("curso"); if (assEntitycurso != null) { } } catch (Exception e) { e.printStackTrace(); } return raiz; } /** * */ public static br.mdarte.exemplo.academico.cd.Curso getCursoByEstudante(long id) throws DAOException { try { Session session = currentSession(); org.hibernate.Query qry = session.createQuery( "select a.curso from br.mdarte.exemplo.academico.cd.Estudante a where a.id = " + id); qry.setCacheable(true); List res = qry.list(); if (res.isEmpty()) { return null; } else { return (br.mdarte.exemplo.academico.cd.Curso) res.get(0); } } catch (HibernateException h) { throw new DAOException(h); } } public void logEvent(String msg, AbstractEntity entity) throws CallbackException { } }