0

我一起使用 Spring-data-Neo4J 和 Spring-data-MongoDb。在 UserRepository 上调用 save 方法会产生错误。任何帮助将不胜感激。

我的域类

@Document
@NodeEntity(partial=true)
public class User implements Serializable {

private static final long serialVersionUID = 1L;

@GraphId Long id; 
@Id
private String userId;
private String firstName;
private String lastName;

@GraphProperty
private String username;

以下是错误日志:

java.lang.ClassCastException: org.springframework.data.neo4j.support.mapping.Neo4jPersistentEntityImpl 不能转换为 org.springframework.data.mongodb.core.mapping.MongoPersistentEntity org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator。 onApplicationEvent(MongoPersistentEntityIndexCreator.java:79) org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:47) org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97) org. springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324) org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:263) org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:165) org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:140) org.springframework.data.neo4j.support.Neo4jTemplate。 getPersistentEntity(Neo4jTemplate.java:573) org.springframework.data.neo4j.support.Neo4jTemplate.getMappingPolicy(Neo4jTemplate.java:470) org.springframework.data.neo4j.support.Neo4jTemplate.getMappingPolicy(Neo4jTemplate.java:631) org. springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:295) org.springframework.data.neo4j.repository.AbstractGraphRepository.save(AbstractGraphRepository.java:106) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun .reflect.NativeMethodAccessorImpl。调用(NativeMethodAccessorImpl.java:39)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)java.lang.reflect.Method.invoke(Method.java:597)org.springframework.data.repository.core.support。 RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:323) org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:308) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation. java:172) org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) org.springframework.dao。support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) $Proxy82 .save(未知来源) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect .Method.invoke(Method.java:597) org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318) org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 172)org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)$Proxy84.save(未知来源)202)$Proxy84.save(未知来源)202)$Proxy84.save(未知来源)

4

1 回答 1

0

我认为这是因为您结合了用于 Spring Data MongoDB 的 @Document 和用于 Spring Data Neo4j 的 @NodeEntity。您无法组合这些注释。

@文档

要充分利用 Spring Data/MongoDB 支持中的对象映射功能,您应该使用 @org.springframework.data.mongodb.core.mapping.Document 注释来注释映射对象。尽管映射框架不需要此注解(即使没有任何注解,您的 POJO 也会正确映射),但它允许类路径扫描器查找和预处理您的域对象以提取必要的元数据。如果你不使用这个注解,你的应用程序在你第一次存储域对象时会受到轻微的性能影响,因为映射框架需要建立它的内部元数据模型,以便它知道你的域对象的属性以及如何坚持他们。看http://static.springsource.org/spring-data/data-mongodb/docs/current/reference/html/

@NodeEntity

@NodeEntity 注解用于将 POJO 类转换为由图形数据库中的节点支持的实体。实体上的字段默认映射到节点的属性。引用其他节点实体(或其集合)的字段通过关系链接。见http://static.springsource.org/spring-data/data-neo4j/docs/2.0.0.RELEASE/reference/html/#d0e1621

于 2012-03-09T12:29:00.620 回答