Sha256: e9f1b1035109a9326cd68b5bdc8966c18e20b7f72f85faf4a43b2ed9a17857ce

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 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> {
	
	protected final EntityManager entityManager;
	
	public Repository(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	public void create(Object entity) {
		entityManager.persist(entity);
	}
	
	public void update(Object entity) {
		entityManager.merge(entity);
	}
	
	public void destroy(Object entity) {
		entityManager.remove(entity);
	}
	
	public T find(Serializable id) {
		return (T) 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

2 entries across 2 versions & 1 rubygems

Version Path
vraptor-scaffold-0.0.1.rc lib/generators/app_generator/templates/Repository.java
vraptor-scaffold-0.0.1.beta5 lib/generators/app_generator/templates/Repository.java