当前/前一个节点关系在Spring Data Neo4j中的表示(链表)

huangapple go评论114阅读模式
英文:

current/previous node relations in spring data neo4j (LinkedList)

问题

我正试图创建一个具有类似于下图的 CURRENT/PREVIOUS 关系的节点链表。

我不确定我的解决方案是否是处理这种情况的正确方式,但为了实现这一目标,我使用了一个方法来填充新消息,创建了两个带有关系的节点,如下所示:

  1. @Builder
  2. @Data
  3. @NoArgsConstructor
  4. public class Person {
  5. @Id
  6. @GeneratedValue
  7. private Long id;
  8. private String name;
  9. @Relationship(type = "LATEST")
  10. private Message message;
  11. void newMessage(Message newMessage) {
  12. newMessage.setPrevious(message);
  13. message = newMessage;
  14. }
  15. }
  16. @Builder
  17. @Data
  18. @NoArgsConstructor
  19. public class Message {
  20. @Id
  21. @GeneratedValue
  22. private Long id;
  23. private String text;
  24. @Relationship(type = "PREVIOUS")
  25. private Message previous;
  26. }

我还创建了一个用于测试这个解决方案的示例代码:

  1. @SpringBootApplication
  2. public class NewsFeedApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(NewsFeedApplication.class, args);
  5. }
  6. @Bean
  7. CommandLineRunner init(PersonRepository personRepository) {
  8. return args -> {
  9. Person personToAdd1 = Person.builder().name("John").build();
  10. personToAdd1.newMessage(Message.builder().text("first message").build());
  11. personToAdd1.newMessage(Message.builder().text("second message").build());
  12. personToAdd1.newMessage(Message.builder().text("third message").build());
  13. personRepository.save(personToAdd1);
  14. personToAdd1.newMessage(Message.builder().text("New message.").build());
  15. personRepository.save(personToAdd1);
  16. };
  17. }
  18. }

我觉得我已经接近成功,但我不知道如何重置先前的 CURRENT 关系,因此我的解决方案产生了如下输出:

因此问题是:

  • 如果我的方法可行,如何移除先前的 CURRENT 关系。
  • 如果这种方法是错误的,如何正确地为节点实现具有 CURRENT/PREVIOUS 关系的链表。
英文:

I’m trying to create linked list of nodes with CURRENT/PREVIOUS relation similar to the picture below.

当前/前一个节点关系在Spring Data Neo4j中的表示(链表)

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:

  1. @Builder
  2. @Data
  3. @NoArgsConstructor
  4. public class Person {
  5. @Id
  6. @GeneratedValue
  7. private Long id;
  8. private String name;
  9. @Relationship(type = "LATEST")
  10. private Message message;
  11. void newMessage(Message newMessage) {
  12. newMessage.setPrevious(message);
  13. message = newMessage;
  14. }
  15. }
  16. @Builder
  17. @Data
  18. @NoArgsConstructor
  19. public class Message {
  20. @Id
  21. @GeneratedValue
  22. private Long id;
  23. private String text;
  24. @Relationship(type = "PREVIOUS")
  25. private Message previous;
  26. }

I also created a sample code to test this solution:

  1. @SpringBootApplication
  2. public class NewsFeedApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(NewsFeedApplication.class, args);
  5. }
  6. @Bean
  7. CommandLineRunner init(PersonRepository personRepository) {
  8. return args -> {
  9. Person personToAdd1 = Person.builder().name("John").build();
  10. personToAdd1.newMessage(Message.builder().text("first message").build());
  11. personToAdd1.newMessage(Message.builder().text("second message").build());
  12. personToAdd1.newMessage(Message.builder().text("third message").build());
  13. personRepository.save(personToAdd1);
  14. personToAdd1.newMessage(Message.builder().text("New message.").build());
  15. personRepository.save(personToAdd1);
  16. };
  17. }
  18. }

I feel like I'm close, but I don't know how to reset the previous CURRENT relation and my solution produces output as:

当前/前一个节点关系在Spring Data Neo4j中的表示(链表)

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" 方法自动删除。

有效解决方案:

  1. public interface PersonRepository extends Neo4jRepository<Supplier, Long> {
  2. @Query("MATCH (n:Person {name: $name})-[r:LATEST]->() DELETE r")
  3. void detachLatestFromPerson(String name);
  4. }
  1. void newMessage(PersonRepository personRepository, Message newMessage) {
  2. personRepository.detachLatestFromPerson(name);
  3. newMessage.setPrevious(message);
  4. message = newMessage;
  5. }

附:我仍然有疑虑,因为我不确定这是否是处理这种情况的好方法,所以如果你知道更好的解决方案,可以随意发布,我们总是可以更换 "最佳答案" 当前/前一个节点关系在Spring Data Neo4j中的表示(链表)

英文:

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:

  1. public interface PersonRepository extends Neo4jRepository&lt;Supplier, Long&gt; {
  2. @Query(&quot;MATCH (n:Person {name: $name})-[r:LATEST]-&gt;() DELETE r&quot;)
  3. void detachLatestFromPerson(String name);
  4. }
  1. void newMessage(PersonRepository personRepository, Message newMessage) {
  2. personRepository.detachLatestFromPerson(name);
  3. newMessage.setPrevious(message);
  4. message = newMessage;
  5. }

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' 当前/前一个节点关系在Spring Data Neo4j中的表示(链表)

huangapple
  • 本文由 发表于 2020年9月1日 21:03:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63688254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定