Sha256: 01ce6355e3abaf14de4ef607942f77a8bf42862e267d0c61897763bf80134012

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

package app.repository;

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 T update(T entity) {
		return 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

6 entries across 6 versions & 2 rubygems

Version Path
heroku-vraptor-scaffold-0.9.0 spec/vraptor-scaffold/generators/app/templates/RepositoryJPA.java
vraptor-scaffold-1.3.0 spec/vraptor-scaffold/generators/app/templates/RepositoryJPA.java
vraptor-scaffold-1.3.0.rc spec/vraptor-scaffold/generators/app/templates/RepositoryJPA.java
vraptor-scaffold-1.2.3 spec/vraptor-scaffold/generators/app/templates/Repository.java
vraptor-scaffold-1.2.1 spec/vraptor-scaffold/generators/app/templates/Repository.java
vraptor-scaffold-1.2.0 spec/vraptor-scaffold/generators/app/templates/Repository.java