Spring @Index 的数据 Neo4j 和 InvalidDataAccessApiUsageException 属性

Spring Data Neo4j and InvalidDataAccessApiUsageException for @Index property

我已将 uuid 属性 添加到我的 SDN 4 基础实体中,现在它看起来像:

@NodeEntity
public abstract class BaseEntity {

    @GraphId
    private Long id;

    @Index(unique = true, primary = true)
    private String uuid;

...

}

现在我的一些测试停止工作了。

我有一个 Commentable 摘要 class:

@NodeEntity
public abstract class Commentable extends BaseEntity {

    private final static String COMMENTED_ON = "COMMENTED_ON";

    @Relationship(type = COMMENTED_ON, direction = Relationship.INCOMING)
    private Set<Comment> comments = new HashSet<>();

    public Set<Comment> getComments() {
        return comments;
    }

    public void addComment(Comment comment) {
        if (comment != null) {
            comments.add(comment);
        }
    }

    public void removeComment(Comment comment) {
        comments.remove(comment);
    }

}

与我的域模型不同 NodeEntity 扩展此 class.. 就像

public class Decision extends Commentable
public class Product extends Commentable

等等

这是我的 SDN 4 存储库

@Repository
public interface CommentableRepository extends GraphRepository<Commentable> {
}

现在当我尝试访问

commentableRepository.findOne(commentableId);

其中 commentableId 是决策 id 或产品 id 它失败并出现以下异常:

org.springframework.dao.InvalidDataAccessApiUsageException: Supplied id does not match primary index type on supplied class.; nested exception is org.neo4j.ogm.session.Neo4jException: Supplied id does not match primary index type on supplied class.
    at org.springframework.data.neo4j.transaction.SessionFactoryUtils.convertOgmAccessException(SessionFactoryUtils.java:154)
    at org.springframework.data.neo4j.repository.support.SessionBeanDefinitionRegistrarPostProcessor.translateExceptionIfPossible(SessionBeanDefinitionRegistrarPostProcessor.java:71)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy175.findOne(Unknown Source)

但是,如果我从 BaseEntity.uuid 字段中删除 @Index(unique = true, primary = true),一切都会开始正常工作。

如何解决这个问题?

你用错了RepositoryGraphRepository 是遗留实施。新实现称为 Neo4jRepository.

尝试:

public interface CommentableRepository extends Neo4jRepository<Commentable, String> {

    ...
}

关键区别在于第二个参数化类型,即用于域 class 的 ID 的类型;在这种情况下 String uuid.