Spring 数据 Neo4j 动态属性和地图作为值

Spring Data Neo4j dynamic properties and Maps as value

在我的 @RelationshipEntity 中,我定义了以下动态属性:

@Properties(prefix = "property", allowCast = true)
private Map<Long, Object> properties;

在不同的 Long 键下我需要存储 Map<String, Object> 对象。

如果我只在一个 Long 键下存储一个 Map<String, Object> 对象,一切正常,例如在 Neo4j 级别上,以下代码:

Map<String, Object> valueProperties1 = new HashMap<>();
valueProperties1.put("active", true);
valueProperties1.put("selfEvaluation", 5);
valueProperties1.put("experience", "practical");

properties.put(1L, valueProperties1);

转换为以下 Neo4j 属性:

{
  "property.1.active": true,
  "property.1.selfEvaluation": 3,
  "property.1.experience": "practical"
}

到目前为止一切正常。

但是当我尝试 select 这个关系实体时,例如通过以下 Spring Data Neo4j 存储库方法:

@Query("MATCH (d:Decision)-[drc:HAS_VALUE_ON]->(ch:Characteristic) WHERE d.id = {decisionId} AND ch.id = {characteristicId} RETURN d, ch, drc")
RelationshipValue findByDecisionOnCharacteristic(@Param("decisionId") Long decisionId, @Param("characteristicId") Long characteristicId);

失败并出现以下异常:

org.springframework.dao.InvalidDataAccessApiUsageException: Error mapping GraphModel to instance of com.decisionwanted.domain.model.neo4j.decision.characteristic.value.RelationshipValue; nested exception is org.neo4j.ogm.exception.core.MappingException: Error mapping GraphModel to instance of com.decisionwanted.domain.model.neo4j.decision.characteristic.value.RelationshipValue
    at org.springframework.data.neo4j.transaction.SessionFactoryUtils.convertOgmAccessException(SessionFactoryUtils.java:126)
    at org.springframework.data.neo4j.repository.support.SessionBeanDefinitionRegistrarPostProcessor.translateExceptionIfPossible(SessionBeanDefinitionRegistrarPostProcessor.java:71)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy156.findByDecisionOnCharacteristic(Unknown Source)
    at com.decisionwanted.domain.dao.decision.characteristic.value.RelationshipValueDaoImpl.findByDecisionOnCharacteristic(RelationshipValueDaoImpl.java:175)
    at com.decisionwanted.domain.dao.decision.characteristic.value.RelationshipValueDaoImpl.create(RelationshipValueDaoImpl.java:41)
    at com.decisionwanted.domain.dao.decision.characteristic.value.RelationshipValueDaoImpl$$FastClassBySpringCGLIB$a98b507.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:112)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
    at com.decisionwanted.domain.dao.decision.characteristic.value.RelationshipValueDaoImpl$$EnhancerBySpringCGLIB$cb25ddb.create(<generated>)
    at com.decisionwanted.domain.dao.decision.characteristic.value.ValueDaoImpl.create(ValueDaoImpl.java:119)
    at com.decisionwanted.domain.dao.decision.characteristic.value.ValueDaoImpl.create(ValueDaoImpl.java:60)
    at com.decisionwanted.domain.dao.decision.characteristic.value.ValueDaoImpl$$FastClassBySpringCGLIB$$db4e63af.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:112)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
    at com.decisionwanted.domain.dao.decision.characteristic.value.ValueDaoImpl$$EnhancerBySpringCGLIB$ce1d2d3.create(<generated>)
    at com.decisionwanted.domain.DecisionCharacteristicIT.testMultivaluedCharacteristicValue(DecisionCharacteristicIT.java:313)
    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:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.neo4j.ogm.exception.core.MappingException: Error mapping GraphModel to instance of com.decisionwanted.domain.model.neo4j.decision.characteristic.value.RelationshipValue
    at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:205)
    at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:132)
    at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:88)
    at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.lambda$executeAndMap(ExecuteQueriesDelegate.java:124)
    at org.neo4j.ogm.session.Neo4jSession.doInTransaction(Neo4jSession.java:539)
    at org.neo4j.ogm.session.Neo4jSession.doInTransaction(Neo4jSession.java:523)
    at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.executeAndMap(ExecuteQueriesDelegate.java:120)
    at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.query(ExecuteQueriesDelegate.java:90)
    at org.neo4j.ogm.session.Neo4jSession.query(Neo4jSession.java:414)
    at sun.reflect.GeneratedMethodAccessor62.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.data.neo4j.transaction.SharedSessionCreator$SharedSessionInvocationHandler.invoke(SharedSessionCreator.java:131)
    at com.sun.proxy.$Proxy112.query(Unknown Source)
    at org.springframework.data.neo4j.repository.query.GraphQueryExecution$SingleEntityExecution.execute(GraphQueryExecution.java:74)
    at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery.doExecute(GraphRepositoryQuery.java:77)
    at org.springframework.data.neo4j.repository.query.AbstractGraphRepositoryQuery.execute(AbstractGraphRepositoryQuery.java:51)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:590)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    ... 66 more
Caused by: java.lang.UnsupportedOperationException: Only String and Enum allowed to be keys, got class java.lang.Long
    at org.neo4j.ogm.typeconversion.MapCompositeConverter.keyInstanceFromString(MapCompositeConverter.java:190)
    at org.neo4j.ogm.typeconversion.MapCompositeConverter.putToMap(MapCompositeConverter.java:143)
    at org.neo4j.ogm.typeconversion.MapCompositeConverter.toEntityAttribute(MapCompositeConverter.java:133)
    at org.neo4j.ogm.typeconversion.MapCompositeConverter.toEntityAttribute(MapCompositeConverter.java:40)
    at org.neo4j.ogm.context.GraphEntityMapper.getCompositeProperties(GraphEntityMapper.java:252)
    at org.neo4j.ogm.context.GraphEntityMapper.createRelationshipEntity(GraphEntityMapper.java:393)
    at org.neo4j.ogm.context.GraphEntityMapper.mapRelationshipEntity(GraphEntityMapper.java:351)
    at org.neo4j.ogm.context.GraphEntityMapper.mapRelationships(GraphEntityMapper.java:325)
    at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:201)
    ... 91 more

我做错了什么以及如何解决?

documentation提到

Supported types for keys in the Map are String and Enum.

这与您的日志中可以找到的消息相同:

Caused by: java.lang.UnsupportedOperationException: Supported types for keys in the Map are String and Enum.

我仍然不确定为什么它首先起作用。有没有可能你在第一种情况下保存的时候没有把实体加载回来? 即使使用单个长键输入,我也会收到错误消息。