0

I have this scenario where one visitor creates a Pending-Visit-Request to a Resident. Then based upon the reply provided by the Resident, the visit is either approved or rejected. But before creating a new request, there's also a possibility that the Visitor might have been granted a Permanently-Approved-Visit permission by the Resident, in which case I don't need to go through the approval cycle.

I want to model my Entities in such a way that I can use inheritance and also, wherein the Pending-Visit-Request Relationship is created from the Visitor to the Resident while the Approval/disapproval happen from the Resident to the Visitor.

Here's the generic Person class:

public abstract class Person extends Entity {

    @Property private String name;

    @Property private String mobileNumber;

    @Property private String emailAddress;

    @Property private String aadhardId;
}

Please note that the Entity class is the same from the spring-OGM examples. Here's a Resident with the relationship types set:

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

And the Visitor:

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

Here's the generic Visit, the PendingVisit and the PermanentlyApprovedVisit Relations in that order:

public abstract class Visit{

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

}

When trying to create a pendingVisit, I first want to check if a PErmanentlyApprovedVisit relationship already exists. I am writing my tests and this is how I test:

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

Lastly, this is the ResidentRepository method:

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

But, when I run it, I keep getting this exception:

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
)

Is there an issue with the kind of modelling that I am trying to acheive here? I read another thread where this was a problem with Prev and Next:

Neo4j TimeTree REST API Previous and Next Navigation

Community
  • 1
  • 1
Sagar
  • 229
  • 4
  • 14
  • Can you draw a picture of your graph model first, and put it at the bginning of your question? – Michael Hunger Sep 17 '15 at 09:16
  • @MichaelHunger I writing junit test cases using neo4j test harness... Don't have any picture of the graph model per se. I can explain further if need be. – Sagar Sep 17 '15 at 15:34
  • it seems you want to map relationship-entities only? Not sure if that's already supported? – Michael Hunger Sep 20 '15 at 00:58

1 Answers1

0

Actually I fixed this problem by refering to the collections that I have created in the individual NodeEntities instead of hitting cypher queries.

Sagar
  • 229
  • 4
  • 14