CRUD em JPA

3 respostas
A

Boa tarde galera

Eu preciso de um exemplo de CRUD em JPA se alguem puder me ajudar

:smiley: Muito Obrigado.

3 Respostas

L
public class UtilsCrud {

	private static EntityManager getEntityManager() {
		return JPASessionManager.getCurrentSession();
	}

	public static <T > T create(T entity) throws Exception {
		EntityManager em = getEntityManager();
		Exception e = null;
		T retornoEntity = null;		
		EntityTransaction t = em.getTransaction();
		try {
			t.begin();
			retornoEntity = em.merge(entity);
			t.commit();
		} catch (Exception ex) {
			if (t.isActive()) {
				em.getTransaction().rollback();
			}
			e = ex;
		}
		if (e != null) {
			throw e;
		}
		return retornoEntity;
	}

	public static <T > T update(T entity) throws Exception {
		EntityManager em = getEntityManager();
		Exception e = null;
		T retornoEntity = null;
		EntityTransaction t = em.getTransaction();
		try {
			t.begin();
			retornoEntity = em.merge(entity);
			t.commit();
		} catch (Exception ex) {
			if (t.isActive()) {
				t.rollback();
			}
			e = ex;
		}
		if (e != null) {
			throw e;
		}
		return retornoEntity;
	}

	public static <T > void delete(T entity) throws Exception {
		EntityManager em = getEntityManager();
		Exception e = null;
		EntityTransaction t = em.getTransaction();
		try {
			t.begin();
			entity = em.merge(entity);
			em.remove(entity);
			t.commit();
		} catch (Exception ex) {
			if (t.isActive()) {
				t.rollback();
			}
			e = ex;
		}
		if (e != null) {
			throw e;
		}
	}

	public static <T > T retrieve(Class<T> entityClass, Object id) throws Exception {
		EntityManager em = getEntityManager();
		Exception e = null;
		T retorno = null;
		EntityTransaction t = em.getTransaction();
		try {
			t.begin();
			retorno = (T) em.find(entityClass, id);
			t.commit();
		} catch (Exception ex) {
			if (t.isActive()) {
				t.rollback();
			}
			e = ex;
		}
		if (e != null) {
			throw e;
		}
		return retorno;
	}

	public static <T > List<T> listAll(Class<T> entityClass, String... orderbys) throws Exception {
		EntityManager em = getEntityManager();
		List retorno = null;
		try {
			String query = "select o from " + entityClass.getSimpleName() + "  o  order by ";

			for (String order : orderbys) {
				query += order + " , ";
			}
			query = query.substring(0, query.length() - 2);
			Query q = em.createQuery(query);
			retorno = q.getResultList();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return retorno;
	}

	public static <T > List<T> listAll(Class<T> entityClass) throws Exception {
		EntityManager em = getEntityManager();
		List retorno = null;
		try {			
			String query = "select o from " + entityClass.getSimpleName() + "  o ";
			Query q = em.createQuery(query);			
			retorno = q.getResultList();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return retorno;
	}

	public static <T > List<T> listByProperty(Class<T> entityClass, String propertyName, Object propertyValue) throws Exception {
		EntityManager em = getEntityManager();
		List retorno = null;
		try {
			String query = "select o from " + entityClass.getSimpleName() + "  o  where o." + propertyName + "  = :propertyValue ";
			Query q = em.createQuery(query).setParameter("propertyValue", propertyValue);

			retorno = q.getResultList();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return retorno;
	}

																	

	public static <T > List<T> listByProperty(Class<T> entityClass, String propertyName, Object propertyValue, String operador, String... orderbys) throws Exception {
		EntityManager em = getEntityManager();
		List retorno = null;
		try {
			String query = null;
			if (!operador.equalsIgnoreCase("like")) {
				query = "select o from " + entityClass.getSimpleName() + "  o  where o." + propertyName + "  " + operador + " :propertyValue ";
			} else {
				query = "select o from " + entityClass.getSimpleName() + "  o  where upper(o." + propertyName + ")  " + operador + " upper(:propertyValue) ";
			}

			if (orderbys != null && orderbys.length > 0) {
				query+= " order by ";
				for (String order : orderbys) {
					query += "o." + order + " , ";
				}
				query = query.substring(0, query.length() - 2);
			}

			Query q = em.createQuery(query).setParameter("propertyValue", propertyValue);

			retorno = q.getResultList();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return retorno;
	}

	public static <T > List<T> listByProperty(Class<T> entityClass, String propertyName, Object propertyValue, String operador) throws Exception {
		EntityManager em = getEntityManager();
		List retorno = null;
		try {
			String query = "select o from " + entityClass.getSimpleName() + "  o  where o." + propertyName + "  " + operador + " :propertyValue ";
			Query q = em.createQuery(query).setParameter("propertyValue", propertyValue);

			retorno = q.getResultList();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return retorno;
	}
R

Pessoal alguém tem algum exemplo de um CRUD com JPA+Hibernate ou TopLink, no Netbeans.
Com as funções incluir, salvar, alterar, excluir, atualizar.

Alberto_Cerqueira

Esse ai em cima não serve???

abraço.

Criado 12 de agosto de 2009
Ultima resposta 8 de jul. de 2012
Respostas 3
Participantes 4