Spring Data JPA Transaction - No Transaction in progress - Spring Data Neo4j

北城余情 提交于 2020-01-17 01:58:06

问题


I think i'm missing something obvious. Iam trying to make a entity persist into a database via a JUnit Test case, however it doesnt seem to be persisting due to no active transaction.

Configuration:

 @Configuration
    @EnableTransactionManagement
       public class TransactionConfig {

    @Inject 
    private EntityManagerFactory entityMangerFactory;

    @Bean
    public JpaTransactionManager transactionManager(){
        return new JpaTransactionManager(entityMangerFactory);
    }

TestCase:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { Application.class })
@ActiveProfiles(CommonConstants.SPRING_PROFILE_TEST)
@IntegrationTest
@WebAppConfiguration
public class UserRepositoryTest {

    @Inject
    UserRepository userRepo;

    @Test
    @Rollback(false)
    @Transactional("transactionManager")
    public void addUser() {
        User user = BootstrapDataPopulator.getUser();
        userRepo.save(user);
        System.out.println(user.getId()); //Successfully outputs the id generate by hibernate
        assertNotNull(user.getId());
    }
}

^This test case executed successfully however I do not see any entiites persisted in the database as expected.

When I change the from userRepo.save(user) to userRepo.saveAndFlush(user) I get the following exception:

javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Spring Boot AutoConfiguration Report: http://dumptext.com/YcGaR3Wf

Names of all Spring Beans Initialized: http://dumptext.com/jp9O6l8v


回答1:


I am using Spring Data Neo4j (SDN) in my application as well. SDN comes with a default class Neo4jConfiguration which has:

@Bean(name = {"neo4jTransactionManager","transactionManager"})
@Qualifier("neo4jTransactionManager")
public PlatformTransactionManager neo4jTransactionManager() throws Exception {
    return new JtaTransactionManagerFactoryBean(getGraphDatabaseService()).getObject();
}

The "transactionManager" overrides the bean defined in my TransactionConfig class. Hence the reason no Entity transaction was in progress. I stopped using the SDN class Neo4jConfiguration. This resolved my issue.



来源:https://stackoverflow.com/questions/28846157/spring-data-jpa-transaction-no-transaction-in-progress-spring-data-neo4j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!