Sha256: c009ae43c36a7ae0f378867ab8716101a324c4ce1558f03c199ca7d1cc3cc780
Contents?: true
Size: 1.07 KB
Versions: 2
Compression:
Stored size: 1.07 KB
Contents
package app.repositories; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.Query; public abstract class Repository<T, I extends Serializable> { protected final EntityManager entityManager; protected final Class<T> clazz; protected Repository(EntityManager entityManager) { this.entityManager = entityManager; @SuppressWarnings("unchecked") Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; this.clazz = clazz; } public void create(T entity) { entityManager.persist(entity); } public void update(T entity) { entityManager.merge(entity); } public void destroy(T entity) { entityManager.remove(entity); } public T find(I id) { return entityManager.find(clazz, id); } public List<T> findAll() { Query query = entityManager.createQuery("from " + clazz.getName()); @SuppressWarnings("unchecked") List<T> resultList = query.getResultList(); return resultList; } }
Version data entries
2 entries across 2 versions & 1 rubygems