Spring数据neo4j和mongodb混淆了

时间:2012-03-08 10:12:04

标签: mongodb mixed spring-data-neo4j

我正在使用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(原生方法)     sun.reflect.NativeMethodAccessorImpl.invoke(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(原生方法)     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(未知来源)

1 个答案:

答案 0 :(得分:0)

我认为这是因为你将Spring Data MongoDB的@Document与Spring Data Neo4j的@NodeEntity结合起来。您无法合并这些注释。

<强> @Document

  

要充分利用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