Skip to content

Instantly share code, notes, and snippets.

@brunodutr
Created March 17, 2019 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunodutr/88944cc749ce6490768926f86a653e94 to your computer and use it in GitHub Desktop.
Save brunodutr/88944cc749ce6490768926f86a653e94 to your computer and use it in GitHub Desktop.
package example.bdutra.jpa;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
@Transactional
public class Repository<T> {
@PersistenceContext(unitName = "ArquillianPU")
public EntityManager em;
private Class<T> entityClass;
public Repository() {
}
public Repository(Class<T> entityClass) {
this.entityClass = entityClass;
}
public void create(T entity) {
em.persist(entity);
}
public void update(T entity) {
em.merge(entity);
}
public void delete(Long id) {
T entity = em.find(entityClass, id);
if (entity != null) {
em.remove(entity);
}
}
public void delete(T entity) {
em.remove(entity);
}
public T find(Long id) {
return em.find(entityClass, id);
}
@SuppressWarnings("unchecked")
public List<T> findAll() {
return em.createQuery("select t from " + entityClass.getSimpleName() + " t").getResultList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment