@Transactional in unit test prevents @PostLoad from being invoked

Viewed 1132

I have the following test:

public class Book
{
     public boolean postLoadInvoked;
     @PostLoad
     private void onPostLoad()
     {
         postLoadInvoked = true;
     }
}

public class MyIntegrationTest extends AbstractIntegrationTest
{
     @Autowired
     private BookDAO bookDAO;

     @Test
     public void loadBooks()
     {
          Book book = bookDAO.findOne(...);
          assertTrue(book.postLoadInvoked);
     }  
}

This test passes as-is, but if I add the @Transactional annotations to the test class, it fails:

@Transactional
@TransactionConfiguration(defaultRollback=true)
public class MyIntegrationTest extends AbstractIntegrationTest

Why does configuring the test with @Transactional affect the JPA callback methods?

EDIT

The DAO is just a Spring Data repository, so has no logic:

public interface BookDAO extends
   JpaRepository<Book, Long>, 
   QueryDslPredicateExecutor<Book> {}

The transaction manager also has a standard configuration:

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

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>com.mangofactory.example</value>
        </list>
    </property>
    <property name="persistenceUnitName" value="spring-test" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
        </bean>
    </property>
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
</bean>
2 Answers
Related