Sha256: d1738a09ac50ba19190898e66c045e9eb00492c45baa5ea6d17b005786438062

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

package <%= options[:package] %>.<%= options[:repositories_package] %>;

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public abstract class GenericRepository<T> {
	
	private final SessionFactory factory;
	protected final Class<T> clazz;

	protected GenericRepository(SessionFactory factory) {
		this.factory = factory;
		
		@SuppressWarnings("unchecked")
		Class<T> clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

		this.clazz = clazz;
	}
	
	protected Session session() {
		return factory.getCurrentSession();
	}
	
	public void add(T entity) {
		session().persist(entity);
	}

	@SuppressWarnings("unchecked")
	public T update(T entity) {
		return (T) session().merge(entity);
	}
	
	public void remove(Long id) {
		session().delete(get(id));
	}
	
	@SuppressWarnings("unchecked")
	public T get(Long id) {
		return (T) session().get(clazz, id);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> all() {
		return session().createQuery("from " + clazz.getName()).list();
	}
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
springmvc-scaffold-1.0.1 lib/springmvc-scaffold/generators/app/templates/orm/Repository-hibernate.java.tt