package database;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * CRUD Repository to manage entities.
 * 
 * @author Fernando Felix do Nascimento Junior
 *
 * @param <T>
 *            Entity model to manage
 */
public class Manager<T extends Model> {

	private Class<T> model;
	private Session session;

	public Manager(Class<T> model, Session session) {
		this.model = model;
		this.session = session;
	}

	public Long save(T o) {
		return (Long) session.save(o);
	}

	public void update(T o) {
		o.setDateUpdated(new Date());
		session.update(o);
	}

	public void saveOrUpdate(T o) {
		o.setDateUpdated(new Date());
		session.saveOrUpdate(o);
	}

	public Query createQuery(String query) {
		return session.createQuery(query);
	}

	public Criteria createCriteria() {
		return session.createCriteria(model);
	}

	public void delete(T o) {
		session.delete(o);
	}

	public T get(Long id) {
		return (T) session.get(model, id);
	}

	@SuppressWarnings("unchecked")
	public List<T> getAll() {
		return createCriteria().list();
	}

	public static <M extends Model> Manager<M> create(Class<M> o, Session session) {
		return new Manager<M>(o, session);
	}

}

@@ -12,16 +12,60 @@
 abase;%0A%0A
+import java.lang.reflect.ParameterizedType;%0A
 import j
@@ -155,16 +155,22 @@
 bernate.
+query.
 Query;%0Ai
@@ -197,16 +197,61 @@
 Session;
+%0Aimport org.hibernate.InstantiationException;
 %0A%0A/**%0A *
@@ -489,16 +489,103 @@
 ssion;%0A%0A
+%09/**%0A%09 * Constructor for generic managers%0A%09 *%0A%09 * @param model%0A%09 * @param session%0A%09 */%0A
 %09public 
@@ -675,24 +675,693 @@
 ession;%0A%09%7D%0A%0A
+%09/**%0A%09 * Constructor for non-generic Manager subclasses.%0A%09 * %7B@link http://stackoverflow.com/questions/6624113/get-type-name-for-generic-parameter-of-generic-class%7D%0A%09 */%0A%09@SuppressWarnings(%22unchecked%22)%0A%09public Manager(Session session) %7B%0A%09%09this.session = session;%0A%09%09try %7B%0A%09%09%09this.model = ((Class%3CT%3E) ((ParameterizedType) getClass().getGenericSuperclass())%0A%09%09%09%09%09.getActualTypeArguments()%5B0%5D);%0A%09%09%7D catch (ClassCastException e) %7B%0A%09%09%09throw new InstantiationException(%22Constructor reserved only for non-generic subclasses. %22 + e.getMessage(),%0A%09%09%09%09%09Manager.class);%0A%09%09%7D%0A%09%7D%0A%0A%09public Class%3CT%3E getModel() %7B%0A%09%09return model;%0A%09%7D%0A%0A%09public Session getSession() %7B%0A%09%09return session;%0A%09%7D%0A%0A
 %09public Long
@@ -1603,16 +1603,19 @@
 ic Query
+%3CT%3E
  createQ
@@ -1669,22 +1669,63 @@
 ry(query
-);%0A%09%7D%0A
+, model);%0A%09%7D%0A%0A%09@SuppressWarnings(%22deprecation%22)
 %0A%09public

Resolve deprecation warnings and constructor for Manager subclasses