spring-data-neo4j的唯一子节点



我目前正在一家保险公司为我的学士论文做一个企业架构管理系统。该公司希望在Neo4J-Graphdatabase中显示所有自制应用程序(Application-Node),并将web服务依赖关系作为关系。除了应用程序之外,公司还希望持久化应用程序(HAS_VERSION-Relationship)的[Maven]版本(Version-Node),因为新版本可能有新的依赖项,也可能失去旧的依赖项(USES-Relationship)。这是我的观点。我想为每个应用程序实例提供唯一的version子节点。如:

应用程序Foo HAS_VERSION 1.0;1.1;2.0和应用栏HAS_VERSION 1.0;2.0;3.0;但是这两个应用程序的版本节点1.0和2.0应该是单独的节点。因此,这个示例应该在graphdatabase中总共显示8个节点(2个应用程序和6个版本)。它们是用spring-data-neo4j实现这一点的直接方法吗?我已经尝试在我的应用程序pojo类的版本集上使用@Unique注释。但这导致每个版本都有唯一的节点。因此,上面的示例显示了数据库中的6个节点(2个应用程序和4个版本)。这两个应用程序都与版本节点1.0、2.0有has_version关系。但是我希望每个应用程序都有明确唯一的Version-"子节点",因为与依赖应用程序的版本的use -关系应该在数据库中直接可见。对于唯一的版本节点,它不是。

我还实现了一个自制的方法来做到这一点。但它不是很有效,因为我确实在数据库上使用了许多写和读操作。一些应用程序有很多版本。@Fetch注释非常浪费资源。现在我很好奇spring-data-neo4j是否已经为我的问题提供了解决方案。他们能有效地解决这个问题吗?

是的,这里有一个Spring Data Neo4j 4(目前在版本M1)的例子。请注意如何控制持久性水平或"深度",以便能够控制相关实体的获取。

Application.java

@NodeEntity
public class Application {
    Long id;
    String name;
    @Relationship(type="HAS_VERSION", direction = "OUTGOING")
    Set<Version> versions = new HashSet<>();
    public Application() {
    }
    public Application(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void addVersion(Version version) {
        versions.add(version);
    }
    public Set<Version> getVersions() {
        return versions;
    }
    public Long getId() {
        return id;
    }
}

Version.java

@NodeEntity
public class Version {
    Long id;
    String versionNumber;
    @Relationship(type = "HAS_VERSION", direction = "INCOMING")
    Application application;

    public Version() {
    }
    public Version(String versionNumber) {
        this.versionNumber = versionNumber;
    }
    public String getVersionNumber() {
        return versionNumber;
    }
    public void setVersionNumber(String versionNumber) {
        this.versionNumber = versionNumber;
    }
    public Application getApplication() {
        return application;
    }
    public void setApplication(Application application) {
        this.application = application;
    }
    public Long getId() {
        return id;
    }
}

存储库

public interface VersionRepository extends GraphRepository<Version> {
}
public interface ApplicationRepository extends GraphRepository<Application>{
}

和一个测试:

     @Test
                public void testDomain() {
                    Application foo = new Application("Foo");
                    Version foo10 = new Version("1.0"); //create a NEW version
                    Version foo11 = new Version("1.1"); //create a NEW version
                    Version foo20 = new Version("2.0"); //create a NEW version
                    foo.addVersion(foo10);
                    foo.addVersion(foo11);
                    foo.addVersion(foo20);
                    applicationRepository.save(foo);
                    Application bar = new Application("Bar");
                    Version bar10 = new Version("1.0"); //create a NEW version
                    Version bar20 = new Version("2.0"); //create a NEW version
                    Version bar30 = new Version("3.0"); //create a NEW version
                    bar.addVersion(bar10);
                    bar.addVersion(bar20);
                    bar.addVersion(bar30);
                    applicationRepository.save(bar);
                    session.clear();
                    assertEquals(2, applicationRepository.count()); //2 apps
                    assertEquals(6, versionRepository.count()); //6 versions
                    session.clear();
                    Application barWithoutVersionsLoaded = 
applicationRepository.findOne(bar.getId(),0); 
       //Depth=0 will only load the application and properties on the application node 
       //but not its related objects. 
       //If you don't specify depth, it defaults to 1
                    assertEquals("Bar", barWithoutVersionsLoaded.getName());
                    assertEquals(0, barWithoutVersionsLoaded.getVersions().size()); //No versions loaded
                }

最新更新