package com.safeluck.application.common.core.dao.impl;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanInstantiationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import com.safeluck.application.common.core.dao.CommonDao;
import com.safeluck.application.common.core.dao.ReturningWork;
import com.safeluck.application.common.core.dao.Work;
import com.safeluck.application.common.core.dao.hibernate.UpperCaseKeyMapRowMapper;
import com.safeluck.application.common.core.helper.search.IPage;
import com.safeluck.application.common.core.helper.search.ISearch;
import com.safeluck.application.common.core.helper.search.dao.ISearchDao;
import com.safeluck.application.common.core.helper.search.utils.HibernateUtil;
import com.safeluck.application.common.util.SpringUtils;
@Repository("commonDao")
public class CommonDaoImpl implements CommonDao {
@SuppressWarnings({ "unchecked" })
@Override
public <T, PK extends Serializable> PK save(T entity) {
if(entity != null){
try {
PK id = (PK) getSession().save(entity);
getSession().flush();
return id;
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}
}else{
return null;
}
}
@Override
public <T> void batchSave(List<T> entitys) {
for (int i = 0; i < entitys.size(); i++) {
getSession().save(entitys.get(i));
if (i % 20 == 0) {
// 20个对象后才清理缓存,写入数据库
getSession().flush();
getSession().clear();
}
}
// 最后清理一下----防止大于20小于40的不保存
getSession().flush();
getSession().clear();
}
@Override
public <T> void batchUpdate(List<T> entitys) {
for (int i = 0; i < entitys.size(); i++) {
getSession().update(entitys.get(i));
if (i % 20 == 0) {
// 20个对象后才清理缓存,写入数据库
getSession().flush();
getSession().clear();
}
}
// 最后清理一下----防止大于20小于40的不保存
getSession().flush();
getSession().clear();
}
@Override
public <T> void saveOrUpdate(T entity) {
try {
getSession().saveOrUpdate(entity);
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}
}
@Override
public <T> void batchSaveOrUpdate(List<T> entitys) {
for (int i = 0; i < entitys.size(); i++) {
getSession().saveOrUpdate(entitys.get(i));
if (i % 20 == 0) {
// 20个对象后才清理缓存,写入数据库
getSession().flush();
getSession().clear();
}
}
// 最后清理一下----防止大于20小于40的不保存
getSession().flush();
getSession().clear();
}
@Override
public <T> void delete(T entity) {
try {
getSession().delete(entity);
getSession().flush();
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}
}
@Override
public <T> void deleteById(Class<T> entityName, Serializable id) {
delete(getEntity(entityName, id));
getSession().flush();
}
@Override
public <T> void batchDeleteByIds(Class<T> entityName, Serializable[] ids) {
if (ids == null || ids.length == 0)
return;
for (Serializable id : ids) {
deleteById(entityName, id);
}
getSession().flush();
}
@Override
public <T> void batchDelete(Collection<T> entitys) {
for (Object entity : entitys) {
getSession().delete(entity);
getSession().flush();
}
}
@Override
public <T> void update(T entity) {
getSession().update(entity);
getSession().flush();
}
@SuppressWarnings({ "unchecked" })
@Override
public <T> T getEntity(Class<T> entityName, Serializable id) {
T t = (T) getSession().get(entityName, id);
if (t != null) {
getSession().flush();
}
return t;
}
@SuppressWarnings({ "unchecked" })
@Override
public <T> List<T> loadAll(Class<T> entityClass) {
Criteria criteria = getSession().createCriteria(entityClass);
return criteria.list();
}
@SuppressWarnings({ "unchecked" })
@Override
public <T> T getEntityByProperty(Class<T> entityClass, String propertyName,
Object value) {
Assert.hasText(propertyName);
return (T) createCriteria(entityClass, getSession(),
Restrictions.eq(propertyName, value)).uniqueResult();
}
@SuppressWarnings({ "unchecked" })
@Override
public <T> List<T> findByProperty(Class<T> entityClass,
String propertyName, Object value) {
Assert.hasText(propertyName);
return (List<T>) createCriteria(entityClass, getSession(),
Restrictions.eq(propertyName, value)).list();
}
@SuppressWarnings({ "unchecked" })
@Override
public <T> List<T> findByPropertyWithOrder(Class<T> entityClass,
String propertyName, Object value, boolean isAsc) {
Assert.hasText(propertyName);
return createCriteria(entityClass, isAsc, getSession(),
Restrictions.eq(propertyName, value)).list();
}
@SuppressWarnings({ "unchecked" })
@Override
public <T> T getEntityHql(String hql) {
T t = null;
Query queryObject = getSession().createQuery(hql);
List<T> list = queryObject.list();
if (list.size() == 1) {
getSession().flush();
t = list.get(0);
} else if (list.size() > 0) {
throw new RuntimeException("查询结果数:" + list.size() + "大于1");
}
return t;
}
@SuppressWarnings("unchecked")
@Override
public <T> T getEntityHql(String hql, Object[] param) {
T t = null;
Query queryObject = getSession().createQuery(hql);
if (param != null && param.length > 0) {
for (int i = 0; i < param.length; i++) {
queryObject.setParameter(i, param[i]);
}
}
List<T> list = queryObject.list();
if (list.size() == 1) {
getSession().flush();
t = list.get(0);
} else if (list.size() > 0) {
throw new RuntimeException("查询结果数:" + list.size() + "大于1");
}
return t;
}
@SuppressWarnings("unchecked")
@Override
public String queryForStringHQL(String hql, Object[] param) {
String t = null;
Query queryObject = getSession().createQuery(hql);
if (param != null && param.length > 0) {
for (int i = 0; i < param.length; i++) {
queryObject.setParameter(i, param[i]);
}
}
List<String> list = queryObject.list();
if (list.size() == 1) {
getSession().flush();
t = list.get(0);
} else if (list.size() > 0) {
throw new RuntimeException("查询结果数:" + list.size() + "大于1");
}
return t;
}
@SuppressWarnings("unchecked")
@Override
public String queryForStringSQL(String sql, Object[] param) {
String t = null;
Query queryObject = getSession().createSQLQuery(sql);
if (param != null && param.length > 0) {
for (int i = 0; i < param.length; i++) {
queryObject.setParameter(i, param[i]);
}
}
List<String> list = queryObject.list();
if (list.size() == 1)