英文:
current/previous node relations in spring data neo4j (LinkedList)
问题
我正试图创建一个具有类似于下图的 CURRENT/PREVIOUS
关系的节点链表。
我不确定我的解决方案是否是处理这种情况的正确方式,但为了实现这一目标,我使用了一个方法来填充新消息,创建了两个带有关系的节点,如下所示:
@Builder
@Data
@NoArgsConstructor
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
@Relationship(type = "LATEST")
private Message message;
void newMessage(Message newMessage) {
newMessage.setPrevious(message);
message = newMessage;
}
}
@Builder
@Data
@NoArgsConstructor
public class Message {
@Id
@GeneratedValue
private Long id;
private String text;
@Relationship(type = "PREVIOUS")
private Message previous;
}
我还创建了一个用于测试这个解决方案的示例代码:
@SpringBootApplication
public class NewsFeedApplication {
public static void main(String[] args) {
SpringApplication.run(NewsFeedApplication.class, args);
}
@Bean
CommandLineRunner init(PersonRepository personRepository) {
return args -> {
Person personToAdd1 = Person.builder().name("John").build();
personToAdd1.newMessage(Message.builder().text("first message").build());
personToAdd1.newMessage(Message.builder().text("second message").build());
personToAdd1.newMessage(Message.builder().text("third message").build());
personRepository.save(personToAdd1);
personToAdd1.newMessage(Message.builder().text("New message.").build());
personRepository.save(personToAdd1);
};
}
}
我觉得我已经接近成功,但我不知道如何重置先前的 CURRENT
关系,因此我的解决方案产生了如下输出:
因此问题是:
- 如果我的方法可行,如何移除先前的
CURRENT
关系。 - 如果这种方法是错误的,如何正确地为节点实现具有 CURRENT/PREVIOUS 关系的链表。
英文:
I’m trying to create linked list of nodes with CURRENT/PREVIOUS
relation similar to the picture below.
I'm not sure if my solution is the right way to handle this scenario, but to achieve that I created two nodes with a single method to populate new messages as below:
@Builder
@Data
@NoArgsConstructor
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
@Relationship(type = "LATEST")
private Message message;
void newMessage(Message newMessage) {
newMessage.setPrevious(message);
message = newMessage;
}
}
@Builder
@Data
@NoArgsConstructor
public class Message {
@Id
@GeneratedValue
private Long id;
private String text;
@Relationship(type = "PREVIOUS")
private Message previous;
}
I also created a sample code to test this solution:
@SpringBootApplication
public class NewsFeedApplication {
public static void main(String[] args) {
SpringApplication.run(NewsFeedApplication.class, args);
}
@Bean
CommandLineRunner init(PersonRepository personRepository) {
return args -> {
Person personToAdd1 = Person.builder().name("John").build();
personToAdd1.newMessage(Message.builder().text("first message").build());
personToAdd1.newMessage(Message.builder().text("second message").build());
personToAdd1.newMessage(Message.builder().text("third message").build());
personRepository.save(personToAdd1);
personToAdd1.newMessage(Message.builder().text("New message.").build());
personRepository.save(personToAdd1);
};
}
}
I feel like I'm close, but I don't know how to reset the previous CURRENT
relation and my solution produces output as:
So the question is:
-
If my approach is okay, how could I remove previous
CURRENT
relation. -
If this approach is wrong, how could I implement linked list with CURRENT/PREVIOUS relations for nodes correctly.
答案1
得分: 0
我找到了丢失的拼图,即分离供应商关系。我不知道为什么事先我假设这个关系应该由 Spring Data 仓库的 "save" 方法自动删除。
有效解决方案:
public interface PersonRepository extends Neo4jRepository<Supplier, Long> {
@Query("MATCH (n:Person {name: $name})-[r:LATEST]->() DELETE r")
void detachLatestFromPerson(String name);
}
void newMessage(PersonRepository personRepository, Message newMessage) {
personRepository.detachLatestFromPerson(name);
newMessage.setPrevious(message);
message = newMessage;
}
附:我仍然有疑虑,因为我不确定这是否是处理这种情况的好方法,所以如果你知道更好的解决方案,可以随意发布,我们总是可以更换 "最佳答案"
英文:
I found the missing puzzle, i.e. detaching the supplier relation. I don't know why I assumed in advance that this relation should be deleted automatically by the spring data repository "save" method.
Working solution:
public interface PersonRepository extends Neo4jRepository<Supplier, Long> {
@Query("MATCH (n:Person {name: $name})-[r:LATEST]->() DELETE r")
void detachLatestFromPerson(String name);
}
void newMessage(PersonRepository personRepository, Message newMessage) {
personRepository.detachLatestFromPerson(name);
newMessage.setPrevious(message);
message = newMessage;
}
PS. I still have doubts, as I'm not sure if that's a good approach to handle this scenario, so if you know a better solution, go ahead and post it, we can always swap the 'best answer'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论