Problem with HibernateTemplate
I am integrating Spring and Hibernate. My spring.xml is:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="mappingResources">
<list>
<value>
resources/User.hbm.xml
<!-- Project.hbm.xml ProjCF.hbm.xml Task.hbm.xml TaskCF.hbm.xml Category.hbm.xml
开发者_开发问答 TaskEstimation.hbm.xml ProjectEstimation.hbm.xml Parameter.hbm.xml StatisticTool.hbm.xml
Report.hbm.xml -->
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="myUserDAO" class="main.java.com.gwt.app.server.User">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
And on my User class:
public void setSessionFactory(SessionFactory sessionFactory){
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public User loadUser(String log){
return (User)hibernateTemplate.load("User", log);
}
The problem is that hibernateTemplate is null, could anyone help me???? Thanks in advance!
You must not call new User()
but you have to ask Spring for the bean (appContext.getBean("myUserDAO", User.class)
)
Define the hibernate template in the spring xml as well don't use the new keyword. Below is sample config and classes.
<bean id="yourHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="yourSessionFactory"/>
</property>
</bean>
<bean id="yourRootDAO" class="woc.dao.base.RootDAOImpl" abstract="true">
<property name="hibernateTemplate" ref="yourHibernateTemplate"/>
</bean>
<bean id="comboValueDAO" parent="yourRootDAO" class="woc.dao.impl.ComboValueDAOImpl" />
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public abstract class RootDAOImpl<T> extends HibernateDaoSupport implements RootDAO<T> {
protected Logger logger = Logger.getLogger(getClass());
private Class<T> clazz;
public RootDAOImpl(Class<T> clazz) {
this.clazz = clazz;
}
public void delete(T entity) {
//Mark entity as deleted
try{
Method setDeletedMethod = clazz.getDeclaredMethod("setDeleted", Boolean.class);
setDeletedMethod.invoke(entity, true);
getHibernateTemplate().saveOrUpdate(entity);
}catch(Exception e){
e.printStackTrace();
}
//actually delete
// getHibernateTemplate().delete(entity);
}
@Override
public void deleteAll(Collection<T> entities) {
getHibernateTemplate().deleteAll(entities);
}
@Override
public void saveOrUpdateAll(Collection<T> entities) {
getHibernateTemplate().saveOrUpdateAll(entities);
}
@SuppressWarnings("unchecked")
@Override
public T get(Serializable id) {
return (T)getHibernateTemplate().get(clazz, id);
}
@SuppressWarnings("unchecked")
@Override
public T load(Serializable id) {
return (T)getHibernateTemplate().load(clazz, id);
}
@SuppressWarnings("unchecked")
@Override
public List<T> find(String hql){
return (List<T>)getHibernateTemplate().find(hql);
}
@Override
public Object execute(HibernateCallback action) {
return getHibernateTemplate().execute(action);
}
@SuppressWarnings("unchecked")
@Override
public List<T> findByNamedParam(String queryString, String paramName,Object value) {
return getHibernateTemplate().findByNamedParam(queryString, paramName, value);
}
@SuppressWarnings("unchecked")
@Override
public List<T> findByNamedParam(String queryString, String[] paramNames,Object[] values) {
return getHibernateTemplate().findByNamedParam(queryString, paramNames, values);
}
@SuppressWarnings("unchecked")
@Override
public List<T> getByExampleList(T entity) {
return getHibernateTemplate().findByExample(entity);
}
@Override
public T getByExampleUnique(T entity) {
return null;
}
@SuppressWarnings("unchecked")
@Override
public List<T> listAll() {
return getHibernateTemplate().loadAll(clazz);
}
@SuppressWarnings("unchecked")
@Override
public List<T> loadAll() {
return getHibernateTemplate().loadAll(clazz);
}
@SuppressWarnings("unchecked")
@Override
public T save(T entity) {
return (T)getHibernateTemplate().save(entity);
}
@Override
public void saveOrUpdate(T entity) {
getHibernateTemplate().saveOrUpdate(entity);
}
@Override
public void update(T entity) {
getHibernateTemplate().update(entity);
}
}
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org.springframework.orm.hibernate3.HibernateCallback;
public interface RootDAO<T> {
public List<T> loadAll();
public T save(T entity);
public void delete(T entity);
public T get(Serializable id);
public T load(Serializable id);
public void saveOrUpdate(T entity);
public void deleteAll(Collection<T> entities);
public void saveOrUpdateAll(Collection<T> entities);
public List<T> find(String hql);
public void update(T entity);
public T getByExampleUnique(T entity);
public List<T> getByExampleList(T entity);
public List<T> listAll();
public Object execute(HibernateCallback action);
public List<T> findByNamedParam(String queryString, String paramName,Object value);
public List<T> findByNamedParam(String queryString, String[] paramNames,Object[] values);
}
public interface ComboValueDAO extends RootDAO<ComboValue>{
}
public class ComboValueDAOImpl extends RootDAOImpl<ComboValue> implements ComboValueDAO {
public ComboValueDAOImpl() {
super(ComboValue.class);
}
}
In your code, getHibernateTemplate() and do whatever you require.
精彩评论