package <%= options[:package] %>.<%= options[:repositories_package] %>; import java.lang.reflect.ParameterizedType; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public abstract class GenericRepository { protected EntityManager em; protected final Class clazz; protected GenericRepository() { @SuppressWarnings("unchecked") Class clazz = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; this.clazz = clazz; } @PersistenceContext public void setEntityManager(EntityManager em) { this.em = em; } public void add(T entity) { em.persist(entity); } public T update(T entity) { return em.merge(entity); } public void remove(Long id) { em.remove(get(id)); } public T get(Long id) { return em.find(clazz, id); } @SuppressWarnings("unchecked") public List all() { return em.createQuery("from " + clazz.getName()).getResultList(); } }