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

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

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.

当前/前一个节点关系在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:

@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:

当前/前一个节点关系在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" 方法自动删除。

有效解决方案:

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;
}

附:我仍然有疑虑,因为我不确定这是否是处理这种情况的好方法,所以如果你知道更好的解决方案,可以随意发布,我们总是可以更换 "最佳答案" 当前/前一个节点关系在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:

public interface PersonRepository extends Neo4jRepository&lt;Supplier, Long&gt; {

    @Query(&quot;MATCH (n:Person {name: $name})-[r:LATEST]-&gt;() DELETE r&quot;)
    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' 当前/前一个节点关系在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:

确定