0

实际上我的问题是当我使用 entitymanager.unwrap(Session.class) 时,它会给出:java.lang.IllegalStateException: No transactional EntityManager available异常,甚至添加@Transactional到方法声明中。

但它在我改变后有效:

entitymanager.getEntityManagerFactory().createEntityManager().unwrap(Session.class)

我不知道为什么。谁能帮我?


这是我用于实体管理器的配置。

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="persistenceUnit" />

    <property name="dataSource" ref="bonecpDataSource" />

    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>

    <property name="packagesToScan">
        <list>
            <value>org.a.domain.entity.**.*</value>
        </list>
    </property>

    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>       
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="current_session_context_class">thread</prop>

            <prop key="hibernate.jdbc.batch_size">50</prop>

            <prop key="hibernate.cache.use_query_cache">false</prop>
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            <prop key="hibernate.generate_statistics">false</prop>

        </props>

    </property>
</bean>

<bean id="jpaTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="jpaTxManager"/>
4

0 回答 0