Spring OGM建模实体

时间:2015-09-17 08:13:06

标签: spring neo4j spring-data spring-data-neo4j-4 neo4j-ogm

我有这样一种情况,即一位访客向居民创建待处理访问请求。然后根据居民提供的答复,访问被批准或拒绝。但在创建新请求之前,也有可能访问者可能已被居民授予永久批准访问权限,在这种情况下,我无需经过审批周期。

我希望以可以使用继承的方式对我的实体进行建模,并且其中,在从居民到访问者的批准/不批准发生时,从访客到居民创建了待处理 - 访问 - 请求关系。

这是通用的Person类:

public abstract class Person extends Entity {

    @Property private String name;

    @Property private String mobileNumber;

    @Property private String emailAddress;

    @Property private String aadhardId;
}

请注意,实体类与spring-OGM示例相同。这是一个设置了关系类型的居民:

@NodeEntity(label = "Resident")
public class Resident extends Person {

    @Autowired
    Session session;

    @Relationship(type = "PENDING-VISIT", direction = Relationship.INCOMING)
    Set<PendingVisit> pendingVisits = new HashSet<>();

    @Relationship(type = "PERMANENTLY-APPROVED-VISIT", direction = Relationship.OUTGOING)
    Set<PermanentlyApprovedVisit> permanentlyApprovedVisits = new HashSet<>();

访客:

@NodeEntity(label="Visitor")
public  class Visitor extends Person {

    @Autowired
    ResidentRepository residentRepository;

    @Relationship(type = "PENDING-VISIT", direction = Relationship.OUTGOING)
    private Set<PendingVisit> pendingVisit = new HashSet<>();

    public Set<PendingVisit> getPendingVisits() {
        return pendingVisit;
    }


    @Relationship(type = "PERMANENTLY-APPROVED-VISIT",direction = Relationship.INCOMING)
    Set<PermanentlyApprovedVisit> permanentlyApprovedVisits = new HashSet<>();

以下是该命令中的通用Visit,PendingVisit和PermanentlyApprovedVisit Relations:

公共抽象类访问{

@GraphId   private Long visitId;

@StartNode private T visitRequester;

@EndNode   private R visitResponder;

@Property  private Date dov;

@RelationshipEntity(type="PENDING-VISIT")
public class PendingVisit extends Visit<Visitor, Resident> {

    public PendingVisit(Visitor visitor, Resident resident){
        super(visitor,resident);
    }

}

@RelationshipEntity(type="PERMANENTLY-APPROVED-VISIT")
public class PermanentlyApprovedVisit extends Visit<Resident,Visitor> {

    private final boolean permanentlyApproved = true;

    public PermanentlyApprovedVisit(Resident resident, Visitor visitor){
        super(resident,visitor);
    }

}

在尝试创建pendingVisit时,我首先要检查PErmanentlyApprovedVisit关系是否已存在。我正在编写我的测试,这就是我测试的方式:

Optional<PermanentlyApprovedVisit> permanentlyApprovedVisit = Optional.ofNullable(residentRepository.findIfVistorApprovedPermanentlyByResident(resident.getId(), this.getId()));
        if(permanentlyApprovedVisit.isPresent())
            return permanentlyApprovedVisit.get();

最后,这是ResidentRepository方法:

@Query(" OPTIONAL MATCH (resident:Resident)-[r:PERMANENTLY-APPROVED-VISIT]→(visitor:Visitor)"+
           " WHERE resident.id = {residentId} AND visitor.id = {visitorId}"+
            "RETURN r")
    public PermanentlyApprovedVisit findIfVistorApprovedPermanentlyByResident(@Param("residentId")long residentId, @Param("visitorId") long visitorId);

但是,当我运行它时,我不断得到这个例外:

org.neo4j.ogm.session.result.ResultProcessingException: Could not initialise res
ponse
        at org.neo4j.ogm.session.response.JsonResponse.parseErrors(JsonResponse.
java:165)
        at org.neo4j.ogm.session.response.JsonResponse.parseColumns(JsonResponse
.java:139)
        at org.neo4j.ogm.session.response.JsonResponse.initialiseScan(JsonRespon
se.java:75)
        at org.neo4j.ogm.session.response.GraphModelResponse.initialiseScan(Grap
hModelResponse.java:69)
        at org.neo4j.ogm.session.response.GraphModelResponse.<init>(GraphModelRe
sponse.java:39)
        at org.neo4j.ogm.session.request.SessionRequestHandler.execute(SessionRe
questHandler.java:57)
        at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.executeAndMap(
ExecuteQueriesDelegate.java:118)
        at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.query(ExecuteQ
ueriesDelegate.java:76)
        at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.queryForObject
(ExecuteQueriesDelegate.java:50)
        at org.neo4j.ogm.session.Neo4jSession.queryForObject(Neo4jSession.java:3
30)
        at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery.
execute(GraphRepositoryQuery.java:73)
        at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery.
execute(GraphRepositoryQuery.java:50)
        at org.springframework.data.repository.core.support.RepositoryFactorySup
port$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:431)
        at org.springframework.data.repository.core.support.RepositoryFactorySup
port$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:409)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:179)
        at org.springframework.data.projection.DefaultMethodInvokingMethodInterc
eptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:179)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.
proceedWithInvocation(TransactionInterceptor.java:99)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.
invokeWithinTransaction(TransactionAspectSupport.java:281)
        at org.springframework.transaction.interceptor.TransactionInterceptor.in
voke(TransactionInterceptor.java:96)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:179)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterc
eptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynami
cAopProxy.java:207)
        at com.sun.proxy.$Proxy106.findIfVistorApprovedPermanentlyByResident(Unk
nown Source)
        at visit.domain.Visitor.sendPendingVisitRequest(Visitor.java:62)
        at visit.domain.DomainTests.shouldCreatePendingVisit(DomainTests.java:89
)

我试图在这里进行的建模是否存在问题?我读了另一个帖子,这是Prev和Next的一个问题:

Neo4j TimeTree REST API Previous and Next Navigation

0 个答案:

没有答案