如何使用spring-data-neo4j-cross-store在neo4j和postgres之间的部分节点上支持跨存储持久性?
我想在我的应用程序中同时使用Postgres和Neo4j。我能够配置它们,但是,我很难持久化部分节点实体(跨存储持久化) 给出以下配置和实体。似乎我能够将传统的RDBMS JPA实体持久化到postgres数据库,并将节点实体持久化到neo4j数据库,但是,当尝试通过跨存储持久化将部分实体持久化到两者时,我会得到下面的堆栈跟踪。它抱怨java.lang.ClassCastException说我的PartialEntity不能转换为NodeBacked。我相信这一定与跨商店配置有关,但我无法找到根本问题 我已经在github上创建了一个示例项目来演示我所面临的问题。请随意在这里查看 部分节点性如何使用spring-data-neo4j-cross-store在neo4j和postgres之间的部分节点上支持跨存储持久性?,spring,spring-boot,spring-data,spring-data-jpa,spring-data-neo4j,Spring,Spring Boot,Spring Data,Spring Data Jpa,Spring Data Neo4j,我想在我的应用程序中同时使用Postgres和Neo4j。我能够配置它们,但是,我很难持久化部分节点实体(跨存储持久化) 给出以下配置和实体。似乎我能够将传统的RDBMS JPA实体持久化到postgres数据库,并将节点实体持久化到neo4j数据库,但是,当尝试通过跨存储持久化将部分实体持久化到两者时,我会得到下面的堆栈跟踪。它抱怨java.lang.ClassCastException说我的PartialEntity不能转换为NodeBacked。我相信这一定与跨商店配置有关,但我无法找到根
@Entity
@Table(name = "partial_entity")
@NodeEntity(partial = true)
public class PartialEntity extends AbstractEntity{
@GraphId Long graphId;
/**
* A free form text description.
*/
@Column(name = "description")
@GraphProperty
private String description;
/**
* The name given by the User
*/
@Column(name = "name", nullable = false)
@GraphProperty
private String name;
//getters and setters omitted
}
@NodeEntity
public class FullNodeEntity{
@GraphId Long graphId;
/**
* A free form text description */
private String description;
/**
* The name given by the User
*/
private String name;
//getters and setters omitted
}
丰满
@Entity
@Table(name = "partial_entity")
@NodeEntity(partial = true)
public class PartialEntity extends AbstractEntity{
@GraphId Long graphId;
/**
* A free form text description.
*/
@Column(name = "description")
@GraphProperty
private String description;
/**
* The name given by the User
*/
@Column(name = "name", nullable = false)
@GraphProperty
private String name;
//getters and setters omitted
}
@NodeEntity
public class FullNodeEntity{
@GraphId Long graphId;
/**
* A free form text description */
private String description;
/**
* The name given by the User
*/
private String name;
//getters and setters omitted
}
用户(JPA实体)
AbstractEntity(提供id和审计内容)
堆栈跟踪
java.lang.ClassCastException: com.example.entity.graph.PartialEntity cannot be cast to org.springframework.data.neo4j.aspects.core.NodeBacked
at org.springframework.data.neo4j.cross_store.support.node.CrossStoreNodeEntityStateFactory.getEntityState(CrossStoreNodeEntityStateFactory.java:55)
at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.copyPropertiesTo(SourceStateTransmitter.java:95)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.write(Neo4jEntityConverterImpl.java:170)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.write(Neo4jEntityPersister.java:179)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:247)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:235)
at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:365)
at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:354)
at org.springframework.data.neo4j.repository.AbstractGraphRepository.save(AbstractGraphRepository.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:414)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:399)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:371)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy113.save(Unknown Source)
at com.example.api.controller.UserController.test(UserController.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:291)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:155)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:102)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration$MetricsFilter.doFilterInternal(MetricFilterAutoConfiguration.java:90)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Neo4jConfig
@Configuration
@EnableTransactionManagement(mode=AdviceMode.ASPECTJ)
@EnableNeo4jRepositories(basePackages = "com.example.repository.graph")
public class Neo4jConfig extends CrossStoreNeo4jConfiguration{
Neo4jConfig(){
setBasePackage("com.example.entity.graph");
}
@Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService(){
return new SpringCypherRestGraphDatabase("http://localhost:7474/db/data","neo4j", "neo4j");
}
}
SpringDataConfig
@Configuration
@EnableJpaAuditing
@EnableTransactionManagement(mode=AdviceMode.ASPECTJ)
@EnableJpaRepositories(basePackages = "com.example.repository.rdbms", entityManagerFactoryRef="entityManagerFactory", transactionManagerRef="transactionManager")
public class SpringDataConfig {
// @Bean
// public AuditorAware<AuditableUser> auditorProvider() {
// return new AuditorAwareImpl();
// }
}
应用程序配置
@SpringBootApplication
public class ApiConfiguration {
/**
* Main entry point for the REST API of the application.
*
* @param args Command line arguments.
*/
public static void main(String[] args) {
SpringApplication.run(ApiConfiguration.class, args);
}
@Bean
@Profile("local")
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
try {
File keystore = new ClassPathResource("config/local/local.keystore").getFile();
connector.setScheme("https");
connector.setSecure(true);
connector.setPort(8443);
protocol.setSSLEnabled(true);
protocol.setKeystoreFile(keystore.getAbsolutePath());
protocol.setKeystorePass("changeit");
protocol.setKeyAlias("tomcat");
} catch (IOException ex) {
throw new IllegalStateException("Unable to access keystore", ex);
}
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
}
一些测试代码
@Autowired
PartialEntityRepository partialEntityRepository;
@Autowired
FullNodeEntityRepository fullNodeEntityRepository;
@Autowired
UserService userService;
@RequestMapping("/test")
public void test() {
User owner = new User();
owner.setFirstName("Tiny");
owner.setLastName("Tim");
owner.setEmail("tiny.tim@test.com");
owner.setRole(Role.USER);
UserDetails userDetails = new UserDetails();
userDetails.setPassword("test");
Optional<User> ownerOpt = userService.create(owner, userDetails); /this persists to postgres
FullNodeEntity fullNodeEntity = new FullNodeEntity();
fullNodeEntity.setName("Test Name");
fullNodeEntity.setDescription("Test Description");
fullNodeEntityRepository.save(fullNodeEntity); //this persists to neo4j
PartialEntity partialEntity = new PartialEntity();
partialEntity.setName("Test Name");
partialEntity.setDescription("Test Description");
partialEntityRepository.save(partialEntity); //This throws the exception
}
@Autowired
部分存储部分存储;
@自动连线
fullnodentityrepository fullnodentityrepository;
@自动连线
用户服务用户服务;
@请求映射(“/test”)
公开无效测试(){
用户所有者=新用户();
所有者:setFirstName(“Tiny”);
所有者:setLastName(“Tim”);
setEmail(“微小的。tim@test.com");
owner.setRole(Role.USER);
UserDetails UserDetails=新的UserDetails();
userDetails.setPassword(“测试”);
可选的ownerOpt=userService.create(owner,userDetails);/这将持续到postgres
FullNodeEntity FullNodeEntity=新的FullNodeEntity();
fullNodeEntity.setName(“测试名称”);
fullNodeEntity.setDescription(“测试说明”);
fullNodeEntityRepository.save(fullNodeEntity);//这将持续到neo4j
PartialEntity PartialEntity=新的PartialEntity();
partialEntity.setName(“测试名称”);
partialEntity.setDescription(“测试说明”);
partialEntityRepository.save(partialEntity);//这会引发异常
}
根据Michael Hunger和Oliver Gierke的说法,跨店持久性是一个没有成功的实验(参见)。对于polyglot持久性,看起来您必须自己滚动;) 根据迈克尔·霍恩和奥利弗·吉尔克(Oliver Gierke)的说法,跨店持久性是一个没有成功的实验(参见)。对于polyglot持久性,看起来您必须自己滚动;) 相关的!相关的相关的有没有关于如何推出我们自己的产品的教程或示例?我们在哪里可以学会这样做?我这么问是因为我想在我的应用程序中同时使用JPA和Neo4J。不幸的是,没有一个例子可以让你自己使用,但有一些公认的方法可以实现最终的一致性。第一种是全局事务管理器,如Atomikos或Bitronix(更多信息),使用消息传递系统,如SQS、RabbitMQ等,以建立最终的一致性。或者使用链式事务管理器(更多信息)使用尽力而为的方法也请参见以下内容。因此@SimonLaw也很有用,是否有关于如何使用我们自己的工具的教程或示例?我们在哪里可以学会这样做?我这么问是因为我想在我的应用程序中同时使用JPA和Neo4J。不幸的是,没有一个例子可以让你自己使用,但有一些公认的方法可以实现最终的一致性。第一种是全局事务管理器,如Atomikos或Bitronix(更多信息),使用消息传递系统,如SQS、RabbitMQ等,以建立最终的一致性。或者使用链式事务管理器(更多信息)使用尽力而为的方法也请参见以下内容。因此@SimonLaw也很有用,是否有关于如何使用我们自己的工具的教程或示例?我们在哪里可以学会这样做?我这么问是因为我想在我的应用程序中同时使用JPA和Neo4J。不幸的是,没有一个例子可以让你自己使用,但有一些公认的方法可以实现最终的一致性。第一种是全局事务管理器,如Atomikos或Bitronix(更多信息),使用消息传递系统,如SQS、RabbitMQ等,以建立最终的一致性。或者使用链式事务管理器(更多信息)使用尽力而为的方法也请参见以下内容,
@Autowired
PartialEntityRepository partialEntityRepository;
@Autowired
FullNodeEntityRepository fullNodeEntityRepository;
@Autowired
UserService userService;
@RequestMapping("/test")
public void test() {
User owner = new User();
owner.setFirstName("Tiny");
owner.setLastName("Tim");
owner.setEmail("tiny.tim@test.com");
owner.setRole(Role.USER);
UserDetails userDetails = new UserDetails();
userDetails.setPassword("test");
Optional<User> ownerOpt = userService.create(owner, userDetails); /this persists to postgres
FullNodeEntity fullNodeEntity = new FullNodeEntity();
fullNodeEntity.setName("Test Name");
fullNodeEntity.setDescription("Test Description");
fullNodeEntityRepository.save(fullNodeEntity); //this persists to neo4j
PartialEntity partialEntity = new PartialEntity();
partialEntity.setName("Test Name");
partialEntity.setDescription("Test Description");
partialEntityRepository.save(partialEntity); //This throws the exception
}