我已将 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 是决策 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)
,一切就会开始正常工作。
如何解决这个问题?
您使用了错误的Repository
。 GraphRepository
是一个遗留实现。新的实现称为 Neo4jRepository
。
尝试:
public interface CommentableRepository extends Neo4jRepository<Commentable, String> {
...
}
主要区别在于第二个参数化类型,它是用于域类的ID
的类型;在本例中为 String uuid
。