【问题标题】:Unique subnodes with spring-data-neo4jspring-data-neo4j 的唯一子节点
【发布时间】:2015-05-19 07:18:35
【问题描述】:

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

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

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

【问题讨论】:

    标签: java spring neo4j spring-data-neo4j


    【解决方案1】:

    是的,下面是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;
        }
    }
    

    版本.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
    
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 2015-11-20
      • 2023-03-22
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 2017-01-04
      相关资源
      最近更新 更多