简体   繁体   English

为什么 JPA 脏检查在 Spring 环境中不起作用?

[英]Why doesn't JPA dirty checking work in Spring environment?

I wonder why JPA Dirty Checking doesn't work in Spring environment.我想知道为什么 JPA 脏检查在 Spring 环境中不起作用。

I have a similar experience with Spring boot environment.我对 Spring 引导环境有类似的经历。 At this time, I got the answer that 'If you register the transaction manager directly, the JpaTransactionManager required for using JPA transactions is not automatically registered'.这时得到的回答是“如果直接注册事务管理器,使用JPA事务所需的JpaTransactionManager不会自动注册”。

@Bean
public DataSourceTransactionManager transactionManager(){
    DataSourceTransactionManager manager = new DataSourceTransactionManager(datasource());
    return manager;
}

In other words, it was because the above bean was manually registered.也就是说,是因为上面的bean是手动注册的。

But now I have to register beans manually because I am in Spring environment.但是现在我必须手动注册 bean,因为我在 Spring 环境中。

So I wrote context.xml as below.所以我写了如下的context.xml。

<bean id="jpaVendorAdapter"  class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
<jpa:repositories base-package="MyPackages" />
<jpa:auditing/>
<bean id ="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource"  ref="dataSource"></property>
    <property name="jpaVendorAdapter"  ref="jpaVendorAdapter"></property>
    <property name="packagesToScan" value="MyPackages"></property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MariaDB103Dialect</prop>
        </props>
    </property>
</bean>

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

In this way, INSERT and SELECT work normally.这样INSERT和SELECT就正常工作了。

However, it cannot detect changes (dirty check).但是,它无法检测更改(脏检查)。 Any advice on what I am missing please?请问我缺少什么建议吗?

add this添加这个

<tx:annotation-driven />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM