@Index属性的Spring Data Neo4j和InvalidDataAccessApiUsageException

时间:2017-02-14 11:05:45

标签: 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;

...

}

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

我有一个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);
    }

}

我的域模型中的不同NodeEntity扩展了这个类..比如

public class Decision extends Commentable
public class Product extends Commentable

这是我的SDN 4存储库

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

我现在尝试访问

commentableRepository.findOne(commentableId);

其中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)

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

如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

您使用的是错误的RepositoryGraphRepository是一项遗留实施。新实现称为Neo4jRepository

尝试:

public interface CommentableRepository extends Neo4jRepository<Commentable, String> {

    ...
}

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