Sha256: 1340e09cd6c704499b4e9686bd8d65bd0a7e49791b21195f517a0ae0925e6cb1

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

package app.repositories;

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<T> {
	
	protected EntityManager em;
	protected final Class<T> clazz;

	protected GenericRepository() {
		@SuppressWarnings("unchecked")
		Class<T> clazz = (Class<T>) ((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<T> all() {
		return em.createQuery("from " + clazz.getName()).getResultList();
	}
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
springmvc-scaffold-1.0.1 spec/springmvc-scaffold/generators/app/templates/RepositoryJPA.java