SSH中有关getHibernateTemplate()为空的错误

本文探讨了Struts2+Spring+Hibernate整合过程中出现的getHibernateTemplate()为空的问题,详细分析了错误原因,并提供了正确的DAO对象实例化方式。
  1. 错误原因:getHibernateTemplate()为空,用DAO的findById()方法时就出抛出空指针异常,原因在于在Struts2 action里手动实例化了DAO对象。
  2. 错误分析:Struts2.1 + Spring2.5.6 + Hibernate 3.3 整合,整合后struts2动作委托给Spring来管理,DAO对象的创建也由spring来负责,如下DAO的导入包的部分
    package com.jiyi.dao;
    
    import java.util.List;
    import org.hibernate.LockMode;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    
    import com.jiyi.model.Rollpictures;

    一方面,DAO对象由Spring来维护及创建,所以要遵循依赖注入的原则来降低偶合度。
    另一方面,下面的HibernateDaoSupport告诉我们,Spring在创建Dao对象的同时还偷偷地为HibernateDaoSupport注入了SessionFacetory,而SessionFacetory对Hibernate的重要性是不言而喻的。它负责管理我们的session对象,session的一次会话可以认为是对数据库的一次操作。
    1/*
    * Copyright 2002-2007 the original author or authors.
    
     *Licensed under the Apache License, Version 2.0 (the "License");
     *you may not use this file except in compliance with the License.
     *You may obtain a copy of the License at
    
     *http://www.apache.org/licenses/LICENSE-2.0
    
      * Unless required by applicable law or agreed to in writing, software
      * distributed under the License is distributed on an "AS IS" BASIS,
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      * See the License for the specific language governing permissions and
      * limitations under the License.
      */
     
     package org.springframework.orm.hibernate.support;
     
     import net.sf.hibernate.HibernateException;
     import net.sf.hibernate.Session;
     import net.sf.hibernate.SessionFactory;
     
     import org.springframework.dao.DataAccessException;
     import org.springframework.dao.DataAccessResourceFailureException;
     import org.springframework.dao.support.DaoSupport;
     import org.springframework.orm.hibernate.HibernateTemplate;
     import org.springframework.orm.hibernate.SessionFactoryUtils;
     
     /**
      * Convenient super class for Hibernate-based data access objects.
      *
      * <p>Requires a {@link net.sf.hibernate.SessionFactory} to be set, providing a
      * {@link org.springframework.orm.hibernate.HibernateTemplate} based on it to
      * subclasses through the {@link #getHibernateTemplate()} method.
      * Can alternatively be initialized directly with a HibernateTemplate,
      * in order to reuse the latter's settings such as the SessionFactory,
      * exception translator, flush mode, etc.
      *
      * <p>This base class is mainly intended for HibernateTemplate usage but can
      * also be used when working with a Hibernate Session directly, for example
      * when relying on transactional Sessions. Convenience {@link #getSession}
      * and {@link #releaseSession} methods are provided for that usage style.
      * 
      * <p>This class will create its own HibernateTemplate instance if a SessionFactory
      * is passed in. The "allowCreate" flag on that HibernateTemplate will be "true"
      * by default. A custom HibernateTemplate instance can be used through overriding
      * {@link #createHibernateTemplate}.
      *
      * @author Juergen Hoeller
      * @since 28.07.2003
      * @see #setSessionFactory
      * @see #getHibernateTemplate
      * @see org.springframework.orm.hibernate.HibernateTemplate
      */
     public abstract class HibernateDaoSupport extends DaoSupport {
    
      private HibernateTemplate hibernateTemplate;
    
    
      /**
       * Set the Hibernate SessionFactory to be used by this DAO.
       * Will automatically create a HibernateTemplate for the given SessionFactory.
       * @see #createHibernateTemplate
       * @see #setHibernateTemplate
       */
      public final void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = createHibernateTemplate(sessionFactory);
      }
    
      /**
       * Create a HibernateTemplate for the given SessionFactory.
       * Only invoked if populating the DAO with a SessionFactory reference!
       * <p>Can be overridden in subclasses to provide a HibernateTemplate instance
       * with different configuration, or a custom HibernateTemplate subclass.
       * @param sessionFactory the Hibernate SessionFactory to create a HibernateTemplate for
       * @return the new HibernateTemplate instance
       * @see #setSessionFactory
       */
      protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
          return new HibernateTemplate(sessionFactory);
      }
    
      /**
       * Return the Hibernate SessionFactory used by this DAO.
       */
      public final SessionFactory getSessionFactory() {
          return (this.hibernateTemplate != null ? this.hibernateTemplate.getSessionFactory() : null);
      }
    
      /**
       * Set the HibernateTemplate for this DAO explicitly,
       * as an alternative to specifying a SessionFactory.
       * @see #setSessionFactory
       */
      public final void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
          this.hibernateTemplate = hibernateTemplate;
      }
    
      /**
          * Return the HibernateTemplate for this DAO,
          * pre-initialized with the SessionFactory or set explicitly.
          * <p><b>Note: The returned HibernateTemplate is a shared instance.</b>
          * You may introspect its configuration, but not modify the configuration
          * (other than from within an {@link #initDao} implementation).
          * Consider creating a custom HibernateTemplate instance via
          * <code>new HibernateTemplate(getSessionFactory())</code>, in which
          * case you're allowed to customize the settings on the resulting instance.
          */
         public final HibernateTemplate getHibernateTemplate() {
           return this.hibernateTemplate;
         }
     
         protected final void checkDaoConfig() {
             if (this.hibernateTemplate == null) {
                 throw new IllegalArgumentException  ("'sessionFactory' or 'hibernateTemplate' is required");
             }
         }
     
     
         /**
          * Obtain a Hibernate Session, either from the current transaction or
          * a new one. The latter is only allowed if the
          * {@link org.springframework.orm.hibernate.HibernateTemplate#setAllowCreate "allowCreate"}
          * setting of this bean's {@link #setHibernateTemplate HibernateTemplate} is "true".
          * <p><b>Note that this is not meant to be invoked from HibernateTemplate code
          * but rather just in plain Hibernate code.</b> Either rely on a thread-bound
          * Session or use it in combination with {@link #releaseSession}.
          * <p>In general, it is recommended to use HibernateTemplate, either with
          * the provided convenience operations or with a custom HibernateCallback
          * that provides you with a Session to work on. HibernateTemplate will care
          * for all resource management and for proper exception conversion.
          * @return the Hibernate Session
          * @throws DataAccessResourceFailureException if the Session couldn't be created
          * @throws IllegalStateException if no thread-bound Session found and allowCreate=false
          * @see org.springframework.orm.hibernate.SessionFactoryUtils#getSession(SessionFactory, boolean)
          */
         protected final Session getSession()
                 throws DataAccessResourceFailureException, IllegalStateException   {
     
             return getSession(this.hibernateTemplate.isAllowCreate());
         }
     
         /**
          * Obtain a Hibernate Session, either from the current transaction or
          * a new one. The latter is only allowed if "allowCreate" is true.
          * <p><b>Note that this is not meant to be invoked from HibernateTemplate code
          * but rather just in plain Hibernate code.</b> Either rely on a thread-bound
          * Session or use it in combination with {@link #releaseSession}.
          * <p>In general, it is recommended to use
          * {@link #getHibernateTemplate() HibernateTemplate}, either with
          * the provided convenience operations or with a custom
          * {@link org.springframework.orm.hibernate.HibernateCallback} that
          * provides you with a Session to work on. HibernateTemplate will care
          * for all resource management and for proper exception conversion.
          * @param allowCreate if a non-transactional Session should be created when no
          * transactional Session can be found for the current thread
          * @return the Hibernate Session
          * @throws DataAccessResourceFailureException if the Session couldn't be created
          * @throws IllegalStateException if no thread-bound Session found and allowCreate=false
          * @see org.springframework.orm.hibernate.SessionFactoryUtils#getSession(SessionFactory, boolean)
          */
         protected final Session getSession(boolean allowCreate)
             throws DataAccessResourceFailureException, IllegalStateException   {
     
             return (!allowCreate ?
                 SessionFactoryUtils.getSession(getSessionFactory(), false) :
                     SessionFactoryUtils.getSession(
                             getSessionFactory(),
                             this.hibernateTemplate.getEntityInterceptor(),
                             this.hibernateTemplate.getJdbcExceptionTranslator()));
         }
     
         /**
          * Convert the given HibernateException to an appropriate exception from the
          * <code>org.springframework.dao</code> hierarchy. Will automatically detect
          * wrapped SQLExceptions and convert them accordingly.
          * <p>Delegates to the
          * {@link org.springframework.orm.hibernate.HibernateTemplate#convertHibernateAccessException}
          * method of this DAO's HibernateTemplate.
          * <p>Typically used in plain Hibernate code, in combination with
          * {@link #getSession} and {@link #releaseSession}.
          * @param ex HibernateException that occured
          * @return the corresponding DataAccessException instance
          * @see org.springframework.orm.hibernate.HibernateTemplate#convertHibernateAccessException
          */
         protected final DataAccessException convertHibernateAccessException(HibernateException ex) {
             return this.hibernateTemplate.convertHibernateAccessException(ex);
         }
     
         /**
          * Close the given Hibernate Session, created via this DAO's SessionFactory,
          * if it isn't bound to the thread (i.e. isn't a transactional Session).
          * <p>Typically used in plain Hibernate code, in combination with
          * {@link #getSession} and {@link #convertHibernateAccessException}.
          * @param session the Session to close
          * @see org.springframework.orm.hibernate.SessionFactoryUtils#releaseSession
          */
         protected final void releaseSession(Session session) {
             SessionFactoryUtils.releaseSession(session, getSessionFactory());
         }
     
     }
    


  3. 解决方法:以注入的方式实例化DAO对象。
内容概要:本文研究了基于DPWMA调制与正负序分离的ANPC三电平并网逆变器前馈控制策略,旨在解决传统三电平逆变器存在的谐波含量高、电网不平衡工况适应性差及动态响应速度不足等问题。通过采用有源中点箝位(ANPC)三电平逆变器拓扑,结合双极性倍频脉宽调制(DPWMA)、正负序分离锁相技术和电网电压前馈控制,构建了一套一体化的高性能并网控制体系。该体系不仅优化了逆变器的开关动作机制,改善了输出电压电流的谐波特性,而且通过精确的相位同步和扰动补偿,显著提高了系统的动态响应能力和抗扰性能。仿真结果显示,所提出的控制策略能有效降低并网谐波含量,提升锁相精度与系统动态稳定性,确保在复杂电网工况下的高质量稳定并网。 适合人群:具备一定电力电子基础知识和仿真技能的研发人员,尤其是从事新能源发电、储能系统、柔性输电等领域研究的专业人士。 使用场景及目标:①研究和开发高性能并网逆变器,特别是针对大功率、高电能质量要求的应用场景;②探索如何通过先进的调制和控制策略来提高并网逆变器对电网扰动的适应性和响应速度;③为相关领域的学术研究和技术开发提供理论依据和实践指导。 阅读建议:建议读者结合实际的仿真软件(如MATLAB/Simulink)进行实践操作,以便更好地理解和掌握文中提到的各种控制策略的具体实现方法。同时,鼓励读者关注最新的研究成果和发展趋势,不断深化对该领域的认识。
摘要 在全球生态环境问题日益严峻、公众环保参与意愿持续提升的背景下,传统环保志愿者招募与管理模式存在信息传播零散、供需对接不畅、管理效率低下等痛点,制约了环保公益事业的规模化发展。为解决上述问题,响应生态保护数字化发展需求,本课题设计实现“守望自然”环保志愿者招募与管理网站,通过数字化手段打通环保组织与志愿者的服务链路,对推动环保公益规范化、高效化发展具有重要的现实意义与实践价值。 该网站采用B/S架构与前后端分离模式开发,前端基于Vue架构建组件化响应式界面;后端以Java为开发语言,采用Spring Boot框架搭建应用,搭配MyBatis作为持久层框架,数据库选用MySQL并遵循第三范式设计7个以上核心数据表。系统涵盖用户与管理员两大核心角色,实现了闭环式环保志愿服务功能:用户端支持注册登录、个人信息管理、活动查询报名、环保知识学习、社区互动、问题反馈及客服咨询等功能,满足用户全流程参与需求;管理员端具备用户管理、用户审核、招募与活动信息发布管理、环保知识内容管理、问题反馈处理、证书模板管理、多维度数据可视化分析及社区内容监管等功能,全面支撑环保组织运营管理。开发过程中集成了Token身份认证、MD5密码加密、ECharts数据可视化等关键技术,融入活动智能推荐、数据驱动决策等创新设计,确保系统功能完备性与实用性。 经功能测试、性能测试及安全测试验证,系统运行稳定可靠,具有良好的易用性、安全性和可扩展性,能够高效满足环保组织的用户招募管理需求与用户的多元化参与需求,有效降低环保组织运营成本,提升用户参与体验,为环保理念传播与公益事业发展提供有力的数字化支撑。 关键词:环保志愿者;招募管理系统;Spring Boot;Vue;数据可视化
特等奖标准成品论文(Word无水印纯净版) 硬核结构:全文包含完整的摘要、问题重述与分析、模型假设、符号说明、模型建立与求解、灵敏度分析及结论。 即插即用:排版严格遵循官方规范,逻辑严密。拿到手即可作为绝佳的高分参考模板,稍作替换与个性化润色即可极速完稿,彻底解决写论文难的痛点。 双源硬核解题代码(Python与MATLAB双版本) 拒绝假代码:提供底层逻辑清晰、模块化设计的全套可运行源码。 全流程覆盖:涵盖从前期数据清洗预处理,到中期核心数学模型训练,再到后期启发式算法寻优。 傻瓜式运行:代码自带详尽的逐行中文注释,并支持一键生成高质量结果可视化图表,编程小白也能轻松复现与二次开发。 全量数据与结果展示表 所有中间处理数据、模型输出参数以及最终结论,均已精细整理成高质量表格。直观呈现性能评估指标与多模型对比分析,可直接作为论文正文或附件使用,极大提升学术说服力。 独家硬核思路解析 深入浅出剖析出题人意图,详细拆解每一小问的数学本质与底层逻辑,让你不仅知其然更知其所以然。 【四大核心产品优势】 高效实用:所有代码与论文均经过严格测试,确保结果精准无误、完全可复现,省去熬夜试错的时间。 全栈覆盖:从思路分析到跑出结果,再到写出高质量论文,提供一站式全流程资料矩阵。 排版辅助:资料内提供专业的论文排版一键转换工具与官方标准模板,告别格式调整的繁琐。 持续迭代:网盘直发,开赛后资料库将持续滚动更新,所有用户均可免费同步获取最新包。 【适用人群】 想要打破建模瓶颈的参赛队长与主攻手;急需高质量底层代码的编程小白;目标直指特等奖需要高分模板对标的精英团队。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值