启动异常:

java.lang.ClassCastException: org.springframework.orm.hibernate4.SessionHolder cannot be cast to org.springframework.orm.hibernate3.SessionHolder

由于hibernate4已经完全可以实现事务了 与spring3.1中的hibernatedao,hibernateTemplete等有冲突,所以spring3.1里已经不提供hibernatedaosupport,hibernateTemplete了,只能用hibernate原始的方式用session。 获得session:getSessionFactory().getCurrentSession()。
 package org.konghao.basic.dao;

 import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
*
* @author zh
*
* @param <T>
*/
public interface IBaseDao<T extends Serializable> { void save(T t);
void update(T t);
int save(String hql,Object[] params);
void delete(T t);
void deleteById(String hql, Object[] params);
int update(String hql,Object[] params);
T get(final Serializable id);
T load(final Serializable id);
public List<T> listNotIn(String hql, Object[] params);
public List<T> getByHqlNotIn(String hql, Object[] params);
List<T> listAll();
List<T> listFenYe(int firstResult, int maxResults);
List<T> listFenYeNotIn(int firstResult, int maxResults, String hql, Object[] params);
long getCount();
long getCount(String hql,Object[] params);
List<T> listByHql(String hql,Object[] params);
T getByHql(String hql,Object[] params);
List<T> listFenYeAddparams(int firstResult, int maxResults,String hql,Object[] params);
List<T> queryPage(Map map,int firstResult, int maxResults,String hql) ;
long getCount(String hql, Map map);
}

Hibernate4方式

 package org.konghao.basic.dao;

 import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import javax.inject.Inject; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory; /**
* BaseDaoImpl不能在类型未确定前直接实例化
*
* @author zh
* @param <T>
*/
public class BaseDao<T extends Serializable> implements IBaseDao<T> { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() {
return sessionFactory;
} @Inject
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} protected Session getSession() {
return sessionFactory.getCurrentSession();
} @SuppressWarnings("rawtypes")
private Class Tclass; @SuppressWarnings("rawtypes")
public BaseDao() {
Type type = this.getClass().getGenericSuperclass();
if (type.toString().indexOf("BaseDao") != -1) {
ParameterizedType type1 = (ParameterizedType) type;
Type[] types = type1.getActualTypeArguments();
setTclass((Class) types[0]);
} else {
type = ((Class) type).getGenericSuperclass();
ParameterizedType type1 = (ParameterizedType) type;
Type[] types = type1.getActualTypeArguments();
setTclass((Class) types[0]);
}
} /**
* 保存对象
*
* @param Object
*/ public void save(T t) {
// this.getHibernateTemplate().save(t)
getSession().save(t); } /**
* 更新对象内容
*
* @param Object
*/
public void update(T t) {
// this.getHibernateTemplate().update(t);
getSession().update(t);
} /**
* 删除对象
*
* @param t
*/
public void delete(T t) {
// this.getHibernateTemplate().delete(t);
getSession().delete(t);
} public void deleteById(String hql, Object[] params) {
Query query = this.getSession().createQuery(hql);
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
query.executeUpdate();
} /**
* ͨ通过ID 得到对象
*
* @param Serializable
*/
@SuppressWarnings("unchecked")
public T get(Serializable id) {
// return (T) this.getHibernateTemplate().get(getTclass(), id);
return (T) getSession().get(getTclass(), id);
} /**
* 取得所有对象
*
*/
@SuppressWarnings("unchecked")
public List<T> listAll() {
String hql = "from " + getTclass().getSimpleName();
return this.getSession().createQuery(hql).list();
} /**
* hql解决方案
*
* @param String
* hql
*
* @param Object
* [] params 参数列表 顺序不能颠倒
*/ @SuppressWarnings("unchecked")
public List<T> listByHql(String hql, Object[] params) {
Query query = this.getSession().createQuery(hql);
if (null == params || params.length == 0) {
return query.list();
} else {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
}
return query.list();
} /**
* 同类分页
*
* @param int firstResult 从第几条开始
* @param int maxResults 要取几条
*/
@SuppressWarnings({ "unchecked" })
public List<T> listFenYe(int firstResult, int maxResults) {
String hql = "from " + getTclass().getSimpleName();
Query query = this.getSession().createQuery(hql)
.setMaxResults(maxResults).setFirstResult(firstResult);
return query.list();
} /**
* 取得行数
*/ public long getCount() {
String hql = "select count(*) from " + getTclass().getSimpleName();
return (Long) this.getSession().createQuery(hql).uniqueResult();
} /**
* 懒加载load
*/
@SuppressWarnings("unchecked")
public T load(Serializable id) {
return (T) getSession().load(getTclass(), id);
} @SuppressWarnings("unchecked")
public T getByHql(String hql, Object[] params) {
Query query = this.getSession().createQuery(hql);
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
return (T) query.uniqueResult();
} @SuppressWarnings("unchecked")
public List<T> getByHqlNotIn(String hql, Object[] params) {
System.out.println("Tclass:" + Tclass);
Query query = this.getSession().createSQLQuery(hql).addEntity(Tclass);
List<T> list = null;
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
list = query.list();
return list;
} @SuppressWarnings("unchecked")
public List<T> listFenYeAddparams(int firstResult, int maxResults,
String hql, Object[] params) {
Query query = this.getSession().createQuery(hql);
List<T> list = null;
if (null == params || params.length == 0) {
list = query.setMaxResults(maxResults).setFirstResult(firstResult)
.list();
}
if (null != params || params.length != 0) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
}
return list;
} @SuppressWarnings("unchecked")
public List<T> listFenYeNotIn(int firstResult, int maxResults, String hql,
Object[] params) {
Query query = this.getSession().createSQLQuery(hql).addEntity(Tclass);
List<T> list = null;
if (null == params || params.length == 0) {
list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
}
if (null != params || params.length != 0) {
for (int i = 0; i < params.length; i++) {
System.out.println("params:" + params[i]);
query.setParameter(i, params[i]);
}
list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
}
return list;
} @SuppressWarnings("unchecked")
public List<T> listNotIn(String hql, Object[] params) {
Query query = this.getSession().createSQLQuery(hql).addEntity(Tclass);
List<T> list = null;
if (null != params || params.length != 0) {
for (int i = 0; i < params.length; i++) {
System.out.println("params:" + params[i]);
query.setParameter(i, params[i]);
}
}
list = query.list();
return list;
} public long getCount(String hql, Object[] params) {
Query query = this.getSession().createQuery(hql);
if (null != params || params.length != 0) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
}
return (Long) query.uniqueResult();
} @SuppressWarnings("unchecked")
public List<T> queryPage(Map map, int firstResult, int maxResults,
String hql) {
StringBuffer buffer = new StringBuffer(hql);
StringBuffer wherestring = new StringBuffer();
Iterator paramnames = map.keySet().iterator();
while (paramnames.hasNext()) {
String paramname = (String) paramnames.next();
String value = null;
value = String.valueOf(map.get(paramname));
if (value != null) {
value = value.trim();
if (value.equals("")) {
continue;
}
}
if (wherestring.length() == 0) {
wherestring.append(" where ");
} else {
wherestring.append(" and ");
}
wherestring.append(paramname).append("=").append(value);
buffer.append(wherestring);
}
Query query = this.getSession().createQuery(buffer.toString());
List<T> list = query.setMaxResults(maxResults).setFirstResult(firstResult).list();
return list;
} public long getCount(String hql, Map map) {
StringBuffer buffer = new StringBuffer(hql);
StringBuffer wherestring = new StringBuffer();
Iterator paramnames = map.keySet().iterator();
while (paramnames.hasNext()) {
String paramname = (String) paramnames.next();
String value = null;
value = String.valueOf(map.get(paramname));
if (value != null) {
value = value.trim();
if (value.equals("")) {
continue;
}
}
if (wherestring.length() == 0) {
wherestring.append(" where ");
} else {
wherestring.append(" and ");
}
wherestring.append(paramname).append("=").append(value);
buffer.append(wherestring);
}
Query query = this.getSession().createQuery(buffer.toString());
return (Long) query.uniqueResult();
} public int update(String hql, Object[] params) {
Query query = this.getSession().createQuery(hql);
if (params == null) {
return query.executeUpdate();
}
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
return query.executeUpdate();
} public int save(String hql, Object[] params) {
Query query = this.getSession().createQuery(hql);
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
return query.executeUpdate();
} public T queryObject(String hql, Object[] params) {
Query query = this.getSession().createQuery(hql);
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
return (T) query.uniqueResult();
} @SuppressWarnings("rawtypes")
public Class getTclass() {
if(Tclass == null){
Type type = this.getClass().getGenericSuperclass();
ParameterizedType type2 = (ParameterizedType) type;
Type[] types = type2.getActualTypeArguments();
Tclass = (Class<?>) types[0];
}
return Tclass;
} @SuppressWarnings("rawtypes")
public void setTclass(Class tclass) {
Tclass = tclass;
}
}
 
05-11 13:46