0


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

Paddy
  • 3,472
  • 5
  • 29
  • 48
  • You are using Spring Boot but at the same time trying hard to work around/against it. You shouldn't need the `@EnableTransactionManagement` and `@EnableJpaRepositories` and as such also not the `@DependsOn`. The hibernate dialect can be set in the `application.properties`. If you put your `persistence.xml` in the `/META-INF` directory it should still be picked up as normal JPA would do. This way you can ditch your custom `RepositotyConfiguration`. It also appears that in your `persistence.xml` you are using JTA instead of local transactions (judging from the stack trace). – M. Deinum Dec 10 '14 at 19:19
  • In your post you left out the `persistence.xml` and the reason why you would still need it. Generally you can transform it completely into java based configuration and simply ditch it. – M. Deinum Dec 10 '14 at 19:21
  • Hi @Deinum, I am trying to work on top of an existing project which has lots of models specified in persistence.xml, and trying to specify their packages for autoconfiguration could mean I have to spend lot of time to do things correctly. Our gradle script is overwriting hibernate.dialect, so I tried to specify in the bean configuration (what's the corresponding spring boot property name)? And as you rightly pointed out, my persistence.xml is indeed using jta-data-source, how do I circumvent this for tests yet still use same persistence.xml for reasons mentioned above? – Paddy Dec 11 '14 at 06:21
  • Then only leave the models in the persistence xml and drop everything else. – M. Deinum Dec 11 '14 at 07:49

0 Answers0