2016-12-15 6 views
1

次の詳細は私の問題を説明します。
フレームワーク:春ブーツ
データベース:のNeo4j組み込みモード
リポジトリ: 特定の深さでfindOneを使用する:GraphRepositoryNeo4jノードエンティティの階層データを生成

以下は、これまでに試してみました、私の組織POJO

@NodeEntity 
public class Organization extends UserBaseEntity { 

    @NotNull(message = "error.Organization.name.notnull") 
    private String name; 
    private String email; 

    @Relationship(type = HAS_SUB_ORGANIZATION) 
    private List<Organization> children = new ArrayList<>(); 

// getter and setters 
} 

です。
例: graphRepo.findOne(organizationId、3);
これは組織の完全なネットワークを返します。

I組織の階層データを生成する必要があります。
再帰的なクエリを作成して組織階層を生成する方法はありません。 まず:


は、私はあなたに私は以下のように自分のアプリケーションに実装ソリューションを提案するだけで、ID、名前、子供(サブ組織)
サンプル・フォーマット

[ 
    { 
     id: 1, 
    name: 'Organization 1', 
     children: [ 
     { id: 2, name: 'Organization 1 ' }, 
     { id: 3, name: 'Organization 2' } 
     ] 
    }, 
    { 
    id: 4, 
    name: 'Organization 2', 
    children: [ 
     { 
      id: 5, 
      name: 'Organization 2 unit' 
     } 

    ] 
    } 
] 
+0

'@ Query'アノテーションでカスタムリポジトリクエリを使用しようとしましたか? – digx1

答えて

1

が必要データをマッピングするためのOrganizationDTOを定義します。

Class OrganizationDTO { 
private String code; 
private String name; 
private List<OrganizationDTO> children; 
//Add setter and getter 

}
その後は、あなたのサイファーのクエリを使用してリポジトリを変更:

@Repository 
public class OrganisationRepositoryImpl implement OrganisationRepository { 

    private final Neo4jUtils neo4jUtils; 


    List<Organisation> findOrg(String orgCode) { 

     Map<String, Object> params = map("orgCode", orgCode); 

     String query = "MATCH (n:Organisation)-[r:HAS_SUB_ORGANIZATION*]->(m:Organisation) "+ 
     " where n.code = {orgCode} return " + 
     " n.code, "+ 
     " n.name, "+ 
     " m is not null haschildren ," + 
     " m as children" + 
    return neo4jUtils.cypher(query, params) 
       .map(this::mapToOrganization) 
       .collect(toList()); 
    } 

    private OrganizationDTO mapToOrganization(Map<String, Object> map) { 
     OrganizationDTO org = new OrganizationDTO(
      map.get("n.code"), map.get("n.name")); 
     if((Boolean)map.get("haschildren")){ 
      org.setChilren(m.get("children"); 
     } 

    } 
} 


を休憩APIが実装されている場合は、OrganizationDTOは、あなたの期待通りにJSON形式を応答します。これがあなたを助けてくれることを願っています。