0

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?

choding
  • 67
  • 8
  • I've put a lot of thought and time into this issue. help. ;( – choding Dec 19 '22 at 07:05
  • Do you actually have transaction management? I so no `tx:annotation-driven` or another means of driving transactions. WIthout transaction nothing will be persisted. – M. Deinum Dec 19 '22 at 07:25
  • I didn't understand what it meant. Which one should I be looking for? – choding Dec 19 '22 at 07:31
  • 1
    Just adding a transaction manager isn't enough, you also need to tell spring where to start and stop transactions. Without that nothing will be persisted. So you need `@Transactional` in your code and `tx:annotation-driven` in your xml to enable annotation based transactions. – M. Deinum Dec 19 '22 at 08:06

1 Answers1

0

add this

<tx:annotation-driven />
choding
  • 67
  • 8