Spring boot data JPA getting NullPointerException related to transaction manager

2k Views Asked by At


At the outset, I have tried the options mentioned in various forums for the same stack trace I get. A few of them did not work while with others (like removing javax.persistence.transactiontype) I did not understand how and where to try it.

I am using Spring Boot data JPA (1.2 RC2) + Hibernate (with a custom persistence.xml). Here is my Application.java

@Configuration
@ComponentScan(<our package>)
@EnableAutoConfiguration(exclude = EmbeddedServletContainerAutoConfiguration.class)
@EnableTransactionManagement
@DependsOn("transactionManager")
@EnableJpaRepositories(transactionManagerRef = "transactionManager")
public class Application {
    public static void main(String[] args) {
        run(Application.class, args);
    }
}

My RepositoryConfiguration (as we have custom persistence.xml - currently need to reuse it)

@Configuration
public class RepositotyConfiguration {
    @Autowired
    private DataSource dataSource;

    @Value("${db.dialect}")
    private String dialectClass;

    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = builder.dataSource(dataSource).
                persistenceUnit("main").build();
        entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        Properties additionalProperties = new Properties();
        additionalProperties.put("hibernate.dialect", dialectClass);
        entityManagerFactory.setJpaProperties(additionalProperties);
        return entityManagerFactory;
    }

    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactoryBean.getObject());
        return txManager;
    }
}

The transactionManager here is created if I do not have a single Repository but the moment I add one, ny tests fail with this exception:

Caused by: java.lang.NullPointerException
    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
    at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
    at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)

My test application context is:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@EnableAutoConfiguration
@TransactionConfiguration(transactionManager = "transactionManager")
@IntegrationTest("server.port:0")
@ActiveProfiles("test")
public abstract class TestApplicationContext  {
    @Autowired
    private WebApplicationContext wac;

    @Value("${local.server.port}")
    private int port;
    ...
}

An example (not the actual) of the repository I try to add (where let's say Item is a model object)

public interface ItemRepository extends CrudReposity<Item, Long> {
    Item findByCode(String code);  // this seems to cause the problem, assume 'code' is field in Item
}

Any pointers will be of utmost help.

EDIT: It now fails only if I add extra method in ItemRepository say Item findByItemCode(String itemCode) where let's say itemCode is a field in Item model, but can't understand why?

Thanks,
Paddy

0

There are 0 best solutions below