package locadora.hibernate;

import java.util.List;

import org.hibernate.Session;

public class Dao<T extends Entidade> {

	protected final Session session;

	private final Class<T> type;

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

	public void update(T obj) {
		this.session.update(obj);
	}

	public void save(T obj) {
		this.session.save(obj);
	}

	public void saveOrUpdate(T obj) {
		this.session.saveOrUpdate(obj);
	}
	
	public void delete(T obj) {
		this.session.delete(obj);
		this.session.flush();
	}

	public void merge(T obj) {
		this.session.merge(obj);
	}

	public T load(Long id) {
		return type.cast(session.load(type, id));
	}

	public T load(T entity) {
		return type.cast(session.load(type, entity.getId()));
	}

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

	public T get(T obj) {
		return type.cast(session.get(type, obj.getId()));
	}

	public List<T> listAll (){
		return (List<T>) session.createCriteria(type).list();
	}
}