spring整合Hibernate报错,请高手相助!
学习spring的自动装配,整合Hibernate 设计思路是先写一个BaseDAO的接口,里面写好增删改查等方法,再写一个BaseDAO的实现类BaseDaoImBaseDAODaoImpl,在这个类里初始化Hibernate的sessionFactory,其余的实现类继承它就能实现各自的增删改查。
spring 版本 4.0,Hibernate 版本 4.1
BaseDaoImBaseDAODaoImpl.java
package com.erp.DAOImpole;
import java.lang.reflect.ParameterizedType;
import
java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import
org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.erp.Base.BaseDAO;
public class BaseDAODaoImpl<T> implements BaseDAO<T> {//4.1版本的Hibernate没有HibernatTemlete所以没有继承
private SessionFactory sessionFactory;
private Class<T> clazz=null;
public BaseDAODaoImpl(){//初始化泛型T
this.clazz = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
System.out.println("Class Name+++++++++"+clazz);
}
protected Session getsession(){//获取sessionFactory 以供子类可以调用
// System.out.println("session"+sessionFactory.getClass());
return this.sessionFactory.openSession();
// return HibernateUtil.getSession();
}
@Override
public void Save(T entity) {
// TODO Auto-generated method stub
//this.hibernateTemplate.save(entity);
System.out.println("记录保存开始");
Session session=null;
Transaction tx=null;
System.out.println(getsession().getClass());
session=getsession();
getsession().save(entity);
tx.commit();
}
@Override
public void Update(Long id) {
// TODO Auto-generated method stub
Object boj=getById(id);
if (boj!=null){
// this.hibernateTemplate.update(boj);
}
}
@Override
public T getById(Long id) {
return null;
// TODO Auto-generated method stub
//return this.hibernateTemplate.get(clazz, id);
}
@Override
public void delete(Long id) {
// TODO Auto-generated method stub
Object boj=getById(id);
if (boj!=null){
// this.hibernateTemplate.delete(boj);
}
}
@Override
public List<T> find(T entity) {
return getsession().