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

108 Views Asked by At

I wonder why JPA Dirty Checking doesn't work in Spring environment.

I have a similar experience with Spring boot environment. 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'.

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

In other words, it was because the above bean was manually registered.

But now I have to register beans manually because I am in Spring environment.

So I wrote context.xml as below.

<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.

However, it cannot detect changes (dirty check). Any advice on what I am missing please?

1

There are 1 best solutions below

0
On

add this

<tx:annotation-driven />