Spring 使用 neo4j 查询的数据
Spring data with neo4j query
查看以下实体。
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}/出席
我想要return所有对象的列表,这些对象与提供的 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);
那么如何通过查询获得这个结果?
请指导,
谢谢。
@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());
});
}
}
不太确定您到底想要什么?
如果您想获得所有人的所有参加或特定人的所有参加。
尽管如此,我已经为两者提供了答案。
创建一个 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);
});
}
查看以下实体。
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}/出席
我想要return所有对象的列表,这些对象与提供的 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);
那么如何通过查询获得这个结果?
请指导, 谢谢。
@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());
});
}
}
不太确定您到底想要什么? 如果您想获得所有人的所有参加或特定人的所有参加。
尽管如此,我已经为两者提供了答案。
创建一个 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);
});
}