gpt4 book ai didi

java - 使用 Neo4j 查询的 Spring 数据

转载 作者:行者123 更新时间:2023-12-01 21:24:32 24 4
gpt4 key购买 nike

请参阅以下实体。

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}/参加

我想返回一个包含与所提供的 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());
});

}


}

关于java - 使用 Neo4j 查询的 Spring 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38474704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com