How to get Spring Data Neo4j and Spring Data JPA to work together?

前端 未结 1 1157
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 15:10

I have an application that does some batch jobs using MySQL and, via REST, Neo4j server version.

I can\'t figure out how to make them to work together correctly: I c

1条回答
  •  逝去的感伤
    2021-01-20 15:38

    Finally I made it.

    Instead of having two different transactionManagers, now I have only one ChainedTransactionManager.

    I removed the transactionManager bean from JpaConfig and the neo4j.xml file, and added the following Neo4jConfig

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
    import org.springframework.data.neo4j.config.JtaTransactionManagerFactoryBean;
    import org.springframework.data.neo4j.config.Neo4jConfiguration;
    import org.springframework.data.neo4j.rest.SpringRestGraphDatabase;
    import org.springframework.data.neo4j.transaction.ChainedTransactionManager;
    import org.springframework.orm.jpa.JpaTransactionManager;
    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    import org.springframework.transaction.PlatformTransactionManager;
    
    @EnableNeo4jRepositories(basePackages = { "it.smartblue.mcba.neo4j.repository" })
    @Configuration
    public class Neo4jConfig extends Neo4jConfiguration {
    
        @Autowired
        LocalContainerEntityManagerFactoryBean entityManagerFactory;
    
        @Bean
        public SpringRestGraphDatabase graphDatabaseService() {
            return new SpringRestGraphDatabase("http://192.168.11.186:7474/db/data");
        }
    
        @Override
        @Bean(name = "transactionManager")
        public PlatformTransactionManager neo4jTransactionManager() throws Exception {
            return new ChainedTransactionManager(new JpaTransactionManager(entityManagerFactory.getObject()),
                    new JtaTransactionManagerFactoryBean(graphDatabaseService()).getObject());
        }
    }
    

    Now I have to use on my methods only the @Transactional annotation

    0 讨论(0)
提交回复
热议问题