@Index属性的Spring Data Neo4j和InvalidDataAccessApiUsageException

ale*_*oid 0 neo4j spring-data-neo4j-4 neo4j-ogm

我已将uuid属性添加到我的SDN 4 Base实体,所以现在它看起来像:

@NodeEntity
public abstract class BaseEntity {

    @GraphId
    private Long id;

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

...

}
Run Code Online (Sandbox Code Playgroud)

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

我有一个Commentable抽象类:

@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);
    }

}
Run Code Online (Sandbox Code Playgroud)

不同于NodeEntity我的域模型扩展这个类..喜欢

public class Decision extends Commentable
public class Product extends Commentable
Run Code Online (Sandbox Code Playgroud)

等等

这是我的SDN 4存储库

@Repository
public interface CommentableRepository extends GraphRepository<Commentable> {
}
Run Code Online (Sandbox Code Playgroud)

现在,当我尝试访问

commentableRepository.findOne(commentableId);
Run Code Online (Sandbox Code Playgroud)

其中commentableId是Decision id或Product 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)
Run Code Online (Sandbox Code Playgroud)

但如果我@Index(unique = true, primary = true)从我的BaseEntity.uuid领域删除一切都开始工作正常.

如何解决这个问题?

小智 5

你使用的是错的Repository.GraphRepository是一项遗留实施.调用新实现Neo4jRepository.

尝试:

public interface CommentableRepository extends Neo4jRepository<Commentable, String> {

    ...
}
Run Code Online (Sandbox Code Playgroud)

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