2

我正在使用spring和hibernate开发一个应用程序。

当我运行我的应用程序时,我收到以下错误消息:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined

在我的上下文应用程序文件中,我有这个:

<bean id="tansactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

我用谷歌搜索了这个问题,我找到了一个我必须改变这一行的解决方案:

<bean id="tansactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

经过 :

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">

但我遇到了另一个问题:

org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [org.springframework.orm.jpa.JpaTransactionManager]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

我怎么解决这个问题 ?

4

2 回答 2

2

您的注释中有错字“tansactionManager”丢失并且“r”、“transactionManager”。我进行了更正,对我来说效果很好。

于 2014-12-11T16:41:49.173 回答
1

如果您使用的是会话工厂,那么这应该可以

<bean id="tansactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

但是如果你想使用 JPA EntityManager,那么你需要

    <bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
             <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

请参阅迁移到spring-3-1-and-hibernate-4-1它包含所需配置的好示例

于 2014-12-11T04:45:28.403 回答