见下文实体。
人员实体
@NodeEntity
public class Person {
@GraphId Long personId;
private String name;
private String surname;
@Relationship(type = "ATTENDS", direction = Relationship.OUTGOING)
private Set<Event> events;
参加实体
@RelationshipEntity(type = "ATTENDS")
public class Attends {
@GraphId
private Long id;
@StartNode
private Person person;
@EndNode
private Event event;
private Date attendingDate;
活动实体
@NodeEntity
public class Event {
@GraphId
private Long eventId;
private String eventName;
@Relationship(type = "ATTENDS", direction = Relationship.INCOMING)
private Set<Person> persons;
这是我的API
/人/ {PERSONID} /出席
我希望返回一个所有对象的列表,其关系是关注所提供的id的人,在下面的示例中,它将是一个事件列表。
[{
"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" }
}]
我尝试以下查询,但未获得结果,
List<Attends> findByPersonPersonId(Long personId);
那么如何通过查询来实现这个结果呢?
请指南, 感谢。
答案 0 :(得分: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());
});
}
}
答案 1 :(得分:1)
不太确定你想要什么? 如果您想获得所有人的所有出席或特定人的所有出席。
尽管如此,我已经为两者提供了答案。
创建一个AttendsRepository并在其中添加一个findByPersonId(String personId)。
public interface AttendsRepository extends GraphRepository<Attends>{
@Query("Match(p:Person {personId:{0}})-[a:ATTENDS]->(e:Event) return a")
Iterable<Attends> findByPersonId(String personId);
}
然后您可以通过
获得所需的结果@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);
});
}