hibernateCallBack()
空指针问题
刚学SPRING,现在在弄hibernateCallBack()遇到了问题
@Service("basedao")
@Transactional
public class basedaoimpl<T> implements basedao<T> {
@Autowired
protected HibernateTemplate Template;
public HibernateTemplate getTemplate() {
return Template;
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public Object uniqueResult(final String hql,final Object[] queryParams) {
// TODO Auto-generated method stub
return getTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws
HibernateException,
SQLException {
// TODO Auto-generated method stub
Query query = session.createQuery(hql);//执行查询
setQueryParams(query, queryParams);//设置查询参数
return query.uniqueResult();
}
});
}
XML:<!-- 定义Hibernate模板对象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
public Book checkBookid(BigDecimal id) {
// TODO Auto-generated method stub
String hql="from Book where id=?";
String paras[]={id.toString()};
if(uniqueResult(hql, paras)!=null){
return (Book) uniqueResult(hql, paras);
}
return null;
}
提示return getTemplate().execute(new HibernateCallback() { 这一行错了,怎么办啊
sessionfactory 应该是注入成功了的,因为我用getTemplate().save(obj);可以成功,求解
------解决方案-------------------- getTemplate()看看返回的是不是为null
------解决方案--------------------