Spring 注入 SessionFactory 并提供 NullPointers 继承

发布于 2024-12-22 01:20:52 字数 3431 浏览 5 评论 0 原文

在我的设计中,我的所有 daos 都继承自父类,该父类包含 hibernateTemplate 字段和 setSessionFactory,它在使用 spring 设置会话时创建 hibernateTemplate

这里的问题是,即使它似乎已设置,但当我实际执行代码和调用 hibernateTemplate 对象的 daos 似乎为 null。然而,当我使用会话工厂注入 Dao 对象而不是父泛型类时,它的工作方式就像一个魅力

AbstractDaoSupport 类的部分

/** The hibernate template. */
private HibernateTemplate hibernateTemplate;

/**
 * Sets the session factory.
 *
 * @param sessionFactory the new session factory
 */
public void setSessionFactory(SessionFactory sessionFactory) {
    this.setHibernateTemplate(new HibernateTemplate(sessionFactory));
}

/**
 * Sets the hibernate template.
 *
 * @param hibernateTemplate the hibernateTemplate to set
 */
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
    this.hibernateTemplate = hibernateTemplate;
}

这是当前有问题的代码,其中 hibernateTemplate 在运行时为 null

  <!-- the DataSource for application usage -->
    <bean id="applicationDataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/taxidb"/>
        <property name="username" value="root"/>
        <property name="password" value="abc"/>
   </bean>      

    <bean id="daoSessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="applicationDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.iit.awt.application.domain.Driver</value>
                <value>com.iit.awt.application.domain.DriverRealTimeCurrentLocation</value>
                <value>com.iit.awt.application.domain.Journey</value>
                <value>com.iit.awt.application.domain.Customer</value>
                <value>com.iit.awt.application.domain.SystemConstants</value>
                <value>com.iit.awt.application.domain.DriverRequest</value>             
                <value>com.iit.awt.application.domain.Account</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>


    <bean id="transactionManager" 
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="daoSessionFactory"/>
    </bean> 

    <!-- JPA Daos -->
    <bean id="abstractDaoSupport" class="com.iit.awt.platform.support.AbstractDaoSupport">
        <property name="sessionFactory" ref="daoSessionFactory" />
    </bean>

这是当代码工作而不是上面“JPA Daos”注释下的最后一段代码还有以下

<bean id="driverLocationDao" class="com.iit.awt.application.dao.impl.DriverLocationDaoImpl">
    <property name="sessionFactory" ref="daoSessionFactory" />
</bean>  

另一件事需要注意的是,该类以前是抽象的,我尝试使用“abstract=true”以及让它成为非抽象的当前类 ?

有谁知道为什么 hibernateTemplate 对象为空吗

非常感谢任何帮助

In my design all of my daos inherit from a parent class, this parent class contains the hibernateTemplate field and a setSessionFactory which creates the hibernateTemplate upon the setting of the session using spring

The problem here is that even though it seems to get set but when I actually execute the code and the daos are called the hibernateTemplate object seems to be null. HOWEVER when I inject the Dao object instead of the parent generic class with the session factory it works like a charm

The part of the AbstractDaoSupport class

/** The hibernate template. */
private HibernateTemplate hibernateTemplate;

/**
 * Sets the session factory.
 *
 * @param sessionFactory the new session factory
 */
public void setSessionFactory(SessionFactory sessionFactory) {
    this.setHibernateTemplate(new HibernateTemplate(sessionFactory));
}

/**
 * Sets the hibernate template.
 *
 * @param hibernateTemplate the hibernateTemplate to set
 */
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
    this.hibernateTemplate = hibernateTemplate;
}

This is the current problematic code where the hibernateTemplate is null when running

  <!-- the DataSource for application usage -->
    <bean id="applicationDataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/taxidb"/>
        <property name="username" value="root"/>
        <property name="password" value="abc"/>
   </bean>      

    <bean id="daoSessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="applicationDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.iit.awt.application.domain.Driver</value>
                <value>com.iit.awt.application.domain.DriverRealTimeCurrentLocation</value>
                <value>com.iit.awt.application.domain.Journey</value>
                <value>com.iit.awt.application.domain.Customer</value>
                <value>com.iit.awt.application.domain.SystemConstants</value>
                <value>com.iit.awt.application.domain.DriverRequest</value>             
                <value>com.iit.awt.application.domain.Account</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>


    <bean id="transactionManager" 
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="daoSessionFactory"/>
    </bean> 

    <!-- JPA Daos -->
    <bean id="abstractDaoSupport" class="com.iit.awt.platform.support.AbstractDaoSupport">
        <property name="sessionFactory" ref="daoSessionFactory" />
    </bean>

And this is when the code works instead of the last pieced of code above under the "JPA Daos" comment the following is there

<bean id="driverLocationDao" class="com.iit.awt.application.dao.impl.DriverLocationDaoImpl">
    <property name="sessionFactory" ref="daoSessionFactory" />
</bean>  

Another thing to note was that the class was previously abstract and I tried that with "abstract=true" as well as having it non-abstract the current way

Does anyone have an idea on why the hibernateTemplate object is null?

Any help is greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十六岁半 2024-12-29 01:20:52

您的问题

public void setSessionFactory(SessionFactory sessionFactory) {
    this.setHibernateTemplate(new HibernateTemplate(sessionFactory));
}

您实际上没有在此处设置sessionFactory,因此它将保持null


更好的方式

我按照下面的方式完成了它,为什么要自己创建模板..

AbstractDAO

public abstract class BaseAbstractGenericDao<EntityType, IDType extends Serializable> extends HibernateDaoSuppor  {

}

HibernateTemplate Bean 定义
使用会话工厂注入

 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <constructor-arg>
            <ref bean="sessionFactory"/>
        </constructor-arg>
    </bean>

HibernateTemplate Bean 定义
使用会话数据源注入和其他属性设置器注入

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
          p:dataSource-ref="dataSource">
        <property name="annotatedClasses">
            <list>...</list>
</property>
<bean>

Your issue

public void setSessionFactory(SessionFactory sessionFactory) {
    this.setHibernateTemplate(new HibernateTemplate(sessionFactory));
}

you are not setting sessionFactory actually here, so it would stay null


Better Way

I'd done it following way, why to create template on our own..

AbstractDAO

public abstract class BaseAbstractGenericDao<EntityType, IDType extends Serializable> extends HibernateDaoSuppor  {

}

HibernateTemplate Bean Defination
with session factory injection

 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <constructor-arg>
            <ref bean="sessionFactory"/>
        </constructor-arg>
    </bean>

HibernateTemplate Bean Defination
with session datasource injection and other property setter injection

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
          p:dataSource-ref="dataSource">
        <property name="annotatedClasses">
            <list>...</list>
</property>
<bean>
过期以后 2024-12-29 01:20:52

谁在您的代码中注入 hibernateTemplate ?如果您要扩展 HibernateDaoSupport ,则只需注入 sessionFactory 。 getHibernateTemplate() 方法在其父类中查找 hibernate 模板。这就是为什么下面的代码可以工作的原因

<bean id="driverLocationDao" class="com.iit.awt.application.dao.impl.DriverLocationDaoImpl">
   <property name="sessionFactory" ref="daoSessionFactory" />
</bean>  

这里不需要 hibernate 模板初始化。如果您不扩展 hibernateDaoSuppor,则必须像 @Jigar Joshi 提到的那样注入模板。

请注意,根据 Spring 3 建议,不建议使用 Hibernate 模板 此处

NOTE: As of Hibernate 3.0.1, transactional Hibernate access code can also be coded 
in plain Hibernate style. Hence, for newly started projects, consider adopting the
standard Hibernate3 style of coding data access objects instead, based on 
SessionFactory.getCurrentSession(). 

Who is injecting the hibernateTemplate in your code ? If you're extending HibernateDaoSupport , you only need to inject the sessionFactory . the getHibernateTemplate() method looks for the hibernate template in it's parent class. That is why the code below works

<bean id="driverLocationDao" class="com.iit.awt.application.dao.impl.DriverLocationDaoImpl">
   <property name="sessionFactory" ref="daoSessionFactory" />
</bean>  

Here hibernate template initialization is not required . If you're not extending hibernateDaoSuppor, you will have to inject the template as @Jigar Joshi has mentioned .

Please note, Hibernate template use is not recommended as per Spring 3 recommendations here

NOTE: As of Hibernate 3.0.1, transactional Hibernate access code can also be coded 
in plain Hibernate style. Hence, for newly started projects, consider adopting the
standard Hibernate3 style of coding data access objects instead, based on 
SessionFactory.getCurrentSession(). 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文