package com.heishan.schoolcloud.dao;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.hibernate.type.Type;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class CommonDAO extends HibernateDaoSupport {
private static final Log MYLOG = LogFactory.getLog(CommonDAO.class);
/**
* save
*
* @param transientInstance
*/
public void save(Object transientInstance) {
MYLOG.debug("deleting " + Object.class.getName());
try {
getHibernateTemplate().save(transientInstance);
MYLOG.debug("save successful");
} catch (RuntimeException re) {
MYLOG.error("save failed", re);
throw re;
}
}
/**
* update
*
* @param transientInstance
*/
public void update(Object transientInstance) {
MYLOG.debug("deleting " + Object.class.getName());
try {
getHibernateTemplate().update(transientInstance);
MYLOG.debug("save successful");
} catch (RuntimeException re) {
MYLOG.error("save failed", re);
throw re;
}
}
/**
* delete
*
* @param persistentInstance
*/
public void delete(Object persistentInstance) {
MYLOG.debug("deleting " + Object.class.getName());
try {
getHibernateTemplate().delete(persistentInstance);
MYLOG.debug("delete successful");
} catch (RuntimeException re) {
MYLOG.error("delete failed", re);
throw re;
}
}
/**
* get Object(such as Integer, Long)
*
* @param hql
* @param objArr
* @param typeArr
* @return
*/
public Object getObjectByHql(final String hql, final Object[] objArr, final Type[] typeArr) {
MYLOG.debug("getObjectByHql: " + hql);
try {
Object obj = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (objArr != null) {
query.setParameters(objArr, typeArr);
}
return query.uniqueResult();
}
});
return obj;
} catch (RuntimeException re) {
MYLOG.error("get failed", re);
throw re;
}
}
/**
* get Object(such as Integer, Long)
*
* @param hql
* @param objArr
* @param typeArr
* @return
*/
public Object getObjectByHql(final String hql) {
return getObjectByHql(hql, null, null);
}
/**
* getList
*
* @param hql
* @param objArr
* @param typeArr
* @return List<Object[]>
*/
public List<?> getListByHql(final String hql, final Object[] objArr, final Type[] typeArr) {
MYLOG.debug("getListByHql: " + hql);
try {
List<?> list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery(hql);
if (objArr != null) {
query.setParameters(objArr, typeArr);
}
List<?> list = query.list();
return list;
}
});
return list;
} catch (RuntimeException re) {
MYLOG.error("get failed", re);
throw re;
}
}
/**
* getList
*
* @param hql
* @return List<Object[]>
*/
public List<?> getListByHql(final String hql) {
return getListByHql(hql, null, null);
}
/**
* 此方法对含有text的字段不支持
*
* @param sql eg: select AHDM,AH from AJ where AHDM='227300000005631'
* @return List<Map>
*/
@SuppressWarnings("unchecked")
public Map<String, Object> getMapBySql(final String sql) {
MYLOG.debug("getListBySql: " + sql);
try {
Object obj = getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql);
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
return query.uniqueResult();
}
});
return (Map<String, Object>) obj;
} catch (RuntimeException re) {
MYLOG.error("get failed", re);
throw re;
}
}
/**
* 此方法对含有text的字段不支持
*
* @param sql eg: select AHDM,AH from AJ where AHDM='227300000005631'
* @return List<Map>
*/
public List<?> getListBySql(final String sql, final Object[] objArr, final Type[] typeArr) {
MYLOG.debug("getListBySql: " + sql);
try {
List<?> list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql);
if (objArr != null) {
query.setParameters(objArr, typeArr);
}
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
List<?> list = query.list();
return list;
}
});
return list;
} catch (RuntimeException re) {
MYLOG.error("get failed", re);
throw re;
}
}
/**
* 此方法对含有text的字段不支持
*
* @param sql eg: select AHDM,AH from AJ where AHDM='227300000005631'
* @return List<Map>
*/
public List<?> getListBySql(final String sql) {
return getListBySql(sql, null, null);
}
/**
* 更新
*
* @param sql
* @return
*/
public int updateSql(final String sql, final Object[] objArr, final Type[] typeArr) {
try {
int i = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql);
if (objArr != null) {
query.setParameters(objArr, typeArr);
}
return query.executeUpdate();
}
});
return i;
} catch (RuntimeException re) {
MYLOG.error("get failed", re);
throw re;
}
}
/**
* 更新
*
* @param sql
* @return
*/
public int updateSql(final String sql) {
return updateSql(sql, null, null);
}
}