/**
 **@author wjlmgqs
 **下午4:46:03
 **BaseDaoImp.java
 **org.wjlmgqs.dao.imp
 **OADemo
 */
package org.wjlmgqs.core.dao.imp;

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

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.wjlmgqs.core.dao.BaseDao;
@SuppressWarnings("unchecked")
public class BaseDaoImp<T> implements BaseDao<T> {
	
	@Resource
	private HibernateTemplate hibernateTemplate = null;
	
	private Class<T> TClass = null;
	
	public BaseDaoImp(){
		ParameterizedType pType = (ParameterizedType) super.getClass().getGenericSuperclass();
		TClass = (Class<T>)pType.getActualTypeArguments()[0];
	}

	public Serializable save(T entity) {
		return hibernateTemplate.save(entity);
	}

	public void saveOrUpdate(T entity) {
		hibernateTemplate.saveOrUpdate(entity);
	}

	public void update(T entity) {
		hibernateTemplate.update(entity);
	}

	public void delete(T entity) {
		hibernateTemplate.delete(entity);
	}

	
	public List<T> list() {
		return hibernateTemplate.find("from "+TClass.getName());
	}

	public T load(Serializable ID) {
		return (T) hibernateTemplate.load(TClass,ID);
	}

	public T get(Serializable ID) {
		return (T) hibernateTemplate.get(TClass,ID);
	}
	
	public List<T> findByNamedQuery(String queryName, Object... values) {
		return hibernateTemplate.findByNamedQuery(queryName, values);
	}
	
	public List<T> find(String queryString) {
		return hibernateTemplate.find(queryString);
	}
	public List<T> find(String queryString, Object... values) {
		return hibernateTemplate.find(queryString, values);
	}
	
	public List<T> findByCriteria(DetachedCriteria criteria) {
		return hibernateTemplate.findByCriteria(criteria);
	}
	public List<T> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults)  {
		return hibernateTemplate.findByCriteria(criteria, firstResult, maxResults);
	}



}
