我正在整合Spring和Hibernate。我的spring.xml是:
<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>
在我的用户班上:
public void setSessionFactory(SessionFactory sessionFactory){
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public User loadUser(String log){
return (User)hibernateTemplate.load("User", log);
}
问题是hibernateTemplate为null,任何人都可以帮助我???? 提前谢谢!
答案 0 :(得分:4)
你不能打电话给new User()
,但你必须向Spring询问bean(appContext.getBean("myUserDAO", User.class)
)
答案 1 :(得分:0)
在spring xml中定义hibernate模板,也不要使用new关键字。下面是示例配置和类。
<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);
}
}
在你的代码中,getHibernateTemplate()并做你需要的任何事情。