Sha256: 17f0bd1e72913bf0bcec0c4fd88c5a9bc454ec5c6fc8d55a8f865e76cc7e687f

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

package app.repositories;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.persistence.EntityManager;

public abstract class Repository<T, I extends Serializable> {
	
	protected final EntityManager entityManager;
	
	public Repository(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	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(getParameterizedClass(), id);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> findAll() {
		return entityManager.createQuery("From " + getParameterizedClass().getName()).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	private Class<T> getParameterizedClass() {
		ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
		return (Class<T>) parameterizedType.getActualTypeArguments()[0];
	}
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vraptor-scaffold-1.0.0 spec/lib/generators/app_generator/templates/Repository.java
vraptor-scaffold-0.0.4 spec/lib/generators/app_generator/templates/Repository.java
vraptor-scaffold-0.0.3 spec/lib/generators/app_generator/templates/Repository.java
vraptor-scaffold-0.0.2 spec/lib/generators/app_generator/templates/Repository.java
vraptor-scaffold-0.0.1 spec/lib/generators/app_generator/templates/Repository.java