0

See bellow entities.

Person Entity

@NodeEntity
public class Person {  

    @GraphId Long personId; 

    private String name; 

    private String surname;

    @Relationship(type = "ATTENDS", direction = Relationship.OUTGOING) 
    private Set<Event> events;

Attends Entity

@RelationshipEntity(type = "ATTENDS")
public class Attends {

    @GraphId
    private Long id;

    @StartNode
    private Person person;

    @EndNode
    private Event event;

    private Date attendingDate;

Event Entity

@NodeEntity
public class Event {

    @GraphId
    private Long eventId;

    private String eventName;

    @Relationship(type = "ATTENDS", direction = Relationship.INCOMING)
    private Set<Person> persons; 

Here is my API

/persons/{personId}/attends

I want return a list of all the object with a relationship of attending to the person with the id provided, in the example below it would be a list of events.

[{
"attends":{
"attendsId":"1234",
"startDate":"98098098",
"endDate":"098098098",
event:{ "eventId":"1234", "name":"ComicCon" }
},
"attends":{
"attendsId":"1235",
"startDate":"984548098",
"endDate":"45454545",
event:{ "eventId":"1235", "name":"AWS Summit" }

}]

I try this following query but not getting result,

List<Attends> findByPersonPersonId(Long personId);

So How can achieve this result by query ?

Please Guide, Thanks.

Parth Solanki
  • 3,268
  • 2
  • 22
  • 41

2 Answers2

2
@NodeEntity
public class Person {

    @GraphId Long id; 

    private String personId;

    private String name; 

    private String surname;

    @Relationship(type = "ATTENDS", direction = Relationship.OUTGOING) 
    private Set<Attends> attendedEvents =new HashSet<Attends>();
}

@NodeEntity
public class Event {

    @GraphId
    private Long id;

    private String eventId;

    private String eventName;

    @Relationship(type = "ATTENDS", direction = Relationship.INCOMING)
    private List<Attends> attendedEvents = new ArrayList<Attends>();
}


@RelationshipEntity(type = "ATTENDS")
public class Attends {

    @GraphId
    private Long id;

    private String attendId;

    @StartNode
    private Person person;

    @EndNode
    private Event event;

    private Date attendingDate;
}

@Repository
public interface PersonRepository  extends GraphRepository<Person>{

     Person findByPersonId(String personId);

}


public class PersonServiceTest extends AbstractTest{

    @Autowired
    PersonRepository personRepository;

    @Test
    public void save(){

        Person person = new Person();
        person.setName("Person1");
        person.setPersonId(UUID.randomUUID().toString());
        person.setSurname("Surname1");

        Event event1 = new Event();
        event1.setEventId(UUID.randomUUID().toString());
        event1.setEventName("Event1");

        Event event2 = new Event();
        event2.setEventId(UUID.randomUUID().toString());
        event2.setEventName("Event2");

        Attends attends1 = new Attends();
        attends1.setAttendId(UUID.randomUUID().toString());
        attends1.setAttendingDate(new Date());
        attends1.setPerson(person);
        attends1.setEvent(event1);

        Attends attends2 = new Attends();
        attends2.setAttendId(UUID.randomUUID().toString());
        attends2.setAttendingDate(new Date());
        attends2.setPerson(person);
        attends2.setEvent(event2);

        person.getAttendedEvents().add(attends1);
        person.getAttendedEvents().add(attends2);

        personRepository.save(person);


    }

    @Test
    public void get(){
        Person person = personRepository.findByPersonId("ebc17b4b-2270-4e78-ac68-87bce5444ef4");

        person.getAttendedEvents().forEach(attendedEvent -> {
            System.out.println(attendedEvent.getEvent());
        });

    }


}
Soumya
  • 1,833
  • 5
  • 34
  • 45
  • 1
    The change is @Relationship(type = "ATTENDS", direction = Relationship.OUTGOING) private Set attendedEvents =new HashSet(); This must reference the relationship entity, not the end node of the relationship entity. – Luanne Jul 21 '16 at 06:31
  • Thanks Soumya , But I need list of Attends not fire query on person repository. `List ` fire query on `AttendsRepository` – Parth Solanki Jul 21 '16 at 07:02
  • Hi @Luanne , can you tell me how can I get list of `Attends` ? – Parth Solanki Jul 21 '16 at 07:08
1

Not very sure what exactly you want ? If you want to get all Attends of all Persons or all Attends for a particular person.

Nevertheless, I have provided answers for both.

Create a AttendsRepository and add a findByPersonId(String personId) in it.

public interface AttendsRepository extends GraphRepository<Attends>{

    @Query("Match(p:Person {personId:{0}})-[a:ATTENDS]->(e:Event) return a")
    Iterable<Attends> findByPersonId(String personId);

}

Then you can get the desired results by

@Test
public void getAllAttends(){

    Iterable<Attends> allAttends = attendsRepository.findAll();
    allAttends.forEach(attend -> {
        System.out.println(attend);
    });

}

@Test
public void getAttendsByPerson(){

    String personId = "283f51e9-9ade-4f46-a005-7353b5211c8b";

    Iterable<Attends> allAttends = attendsRepository.findByPersonId(personId);
    allAttends.forEach(attend -> {
        System.out.println(attend);
    });

}
Soumya
  • 1,833
  • 5
  • 34
  • 45
  • Thank you so much man, That's what I want , Now I understand how to make query like this...Appreciate your help!!! – Parth Solanki Jul 21 '16 at 07:56
  • Hi Soumya , and one more thing we cant create query on graphId directly? why we are generating UUID ? – Parth Solanki Jul 21 '16 at 09:21
  • 1
    Q) Why are we generating UUID ? Creating a new id other than the database id was proposed by a Developer Advocate at Neo Technology named Brian Underwood. The link can be found at http://stackoverflow.com/questions/22369520/neo4j-get-node-by-id as a comment to an answer. – Soumya Jul 21 '16 at 19:06
  • 1
    Q) Can we create query on graphId ? Yes we can, @Query("Match(p:Person)-[a:ATTENDS]->(e:Event) where ID(p)={0} return a") Iterable findByPersonDBId(Long dbId); – Soumya Jul 21 '16 at 19:25
  • If anybody is starting with Neo4J SDN, you can find an excellent tutorial by Luanne at https://www.airpair.com/neo4j/posts/the-essence-of-spring-data-neo4j-4 – Soumya Jul 26 '16 at 11:23