Your DAO implementation is not leveraging its superclass correctly.
- You're injecting a Spring
FactoryBean<SessionFactory>
when it really needs a HibernateSessionFactory
. - It's not actually using the injected dependency.
- You're attempting to use
HibernateDaoSupport#getHibernateTemplate()
without giving it a reference to aSessionFactory
or aHibernateTemplate
.
Note that HibernateDaoSupport
will create its own HibernateTemplate
as long as you give it a SessionFactory
.
I suggest you make the following changes:
- Remove
AnnotationSessionFactoryBean sessionFactory
fromUserDaoImpl
- Fix configuration of
UserDaoImpl
by leveraging its inheritedsetSessionFactory(SessionFactory)
method
So you actually end up with this DAO code:
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Override
public void addUser(UserMap userMap) {
getHibernateTemplate().save(userMap);
}
@Override
public List<User> findAllUser() {
return getHibernateTemplate().find("from User");
}
@Override
public void deleteUser(UserMap user) {
getHibernateTemplate().delete(user);
}
@Override
public void updateUser(UserMap user) {
getHibernateTemplate().update(user);
}
}
And this configuration code:
@Configuration
@ComponentScan(basePackages = {"com.argus.intenew"})
public class Webconfig {
//snip...
@Bean
public UserDaoImpl userDao() {
UserDaoImpl userDao=new UserDaoImpl();
userDao.setSessionFactory(sessionFactory().getObject());
return userDao;
}
}
Note that it's ok to return the FactoryBean
from the sessionFactory()
configuration method since Spring will detect and utilize its lifecycle methods (via InitializingBean
and DisposableBean
), but you need to accommodate by calling the FactoryBean
method getObject()
yourself.