2016-10-17 5 views
0

私はpostgresデータベースに以下のレコードを持っています。 parent_pkは、親子関係のpkに関連しています。 pk = 1はすべての子供の親であり、直接的および間接的です。深い再帰親子関係からのJSONの構築

pk    name    type   parent_pk 
---   ----    ----   --------- 
1    hnumber101  house   1 
2    hnumber201  house   1 
791   dodge_charger vehicle   1 
801   mustang   vehicle   791 
595020   civic   vehicle   2 
10077661099 john    user   10046725614 
10046725614 mesto   dev    1 
801   shen    house   791 
44444444  crep    house   10046725614 
22222222  keper   user   10046725614 
11111111  show    house   10046725614 
84257651  shen    house   801 
11    lemp    house   2 

そして私は私の現在のコードでは、次のformat-

{ 
    "children" : [ 
    { "pk" : "1", "name" : "hnumber101", "children" : [ 
     { "pk" : "10046725614", "name" : "mesto", "children" : [ 
      { "pk" : "10077661099", "name" : "john", "children" : [] }, 
      { "pk" : "44444444", "name" : "crep", "children" : [] }, 
      { "pk" : "22222222", "name" : "keper", "children" : [] }, 
      { "pk" : "11111111", "name" : "show", "children" : [] } 
     ] } 
     ] }, 
     { "pk" : "791", "name" : "dodge_charger", "children" : [ 
     { "pk" : "801", "name" : "mustang", "children" : [ 
      { "pk" : "84257651", "name" : "shen", "children" : [ 
      ] } 
     ] }, 
     { "pk" : "2", "name" : "hnumber201", "children" : [ 
      { "pk" : "595020", "name" : "civic", "children" : [] }, 
      { "pk" : "11", "name" : "lemp", "children" : [] } 
     ] } 
     ] } 
    ] } 
    ] 
} 

に上記のうち、JSONを生成したい、私はPK = 1の子の子だけを取得することができことができます。 しかし、深い再帰は起こっていません。

Collection<GatherEntity> gatherEntityChildren= gatherManager.findByParentGatherId(1); 
getRecursiveGatherFromParent(gatherEntityChildren, gatherListParent); 

private List<Gather> getRecursiveGatherFromParent(Collection<GatherEntity> gatherEntityChildren, List<Gather> gatherListParent) throws JSONException {  


     if(gatherEntityChildren != null && gatherEntityChildren.size() > 0) { 
      for (Iterator<gatherEntity> iterator = gatherEntityChildren.iterator(); iterator.hasNext();) { 
       GatherEntity gatherEntity = (GatherEntity) iterator.next(); 

       Gather gather = getGatherFromEntity(gatherEntity); 
       List<Gather> gatherChildren = populateGatherAndChild(gatherEntity); 
       gather.setChildren(new HashSet<Gather>(gatherChildren)); 
       gatherListParent.add(gather); 
      } 
     } 
     return gatherListParent; 
    } 

    private List<Gather> populateGatherAndChild(GatherEntity gatherEntity) { 
     Collection<GatherEntity> gatherEntityChildren= gatherManager.findByParentGatherId(gatherEntity.getGatherId()); 
     List<Gather> gatherList = gatherEntityChildren.stream().map(UserAPIImpl::getGatherFromEntity).collect(Collectors.toList());  
     return gatherList; 
    } 

    private static Gather getGatherFromEntity(GatherEntity gatherEntity) { 
     Gather gather = new Gather(); 
     gather.setGatherId(gatherEntity.getGatherId()); 
     gather.setName(gatherEntity.getName()); 
     return gather; 
    } 
+0

を。純粋なPostgreSQLソリューションの場合は、http://stackoverflow.com/a/25683134/1499698 – pozs

答えて

1

あなたは子供の再帰呼び出し見逃している:私はあなたが興味がない

 if(gatherEntityChildren != null && gatherEntityChildren.size() > 0) { 
     for (Iterator<gatherEntity> iterator = gatherEntityChildren.iterator(); iterator.hasNext();) { 
      GatherEntity gatherEntity = (GatherEntity) iterator.next(); 

      Gather gather = getGatherFromEntity(gatherEntity); 
      Collection<GatherEntity> gatherChildren = populateGatherAndChild(gatherEntity); 

      List<Gather> gatherList = gatherEntityChildren.stream().map(UserAPIImpl::getGatherFromEntity).collect(Collectors.toList()); 
      gather.setChildren(new HashSet<Gather>(gatherList)); 
      gatherListParent.add(gather); 

      getRecursiveGatherFromParent(gatherChildren, gatherListParent); 
     } 
    } 
    return gatherListParent; 
} 

private List<GatherEntity> populateGatherAndChild(GatherEntity gatherEntity) { 
    return gatherManager.findByParentGatherId(gatherEntity.getGatherId()); 
} 
+0

をご覧ください。これはStackOverErrorを与えています。私はこれはpk = 1とparent_pk = 1だから 'findByParentGatherId(1)'は常に同じだと思う。言い換えれば、「1」の子供も「1」を持っています。私はいくつかの条件を入れて、parent = 1が設定されたら、もう一度それを処理しないようにする必要があります。 –

+0

私の再帰関数 'gatherEntityChildren'の最初の引数は、次回以降の再帰呼び出しで以前の値を上書きしています。それが今私が直面している問題です。 –

+0

おもしろいことに、なぜあなたはすべてのレベルで子供の新しいリストを作成するのか分かりません。問題が見つかったら教えてください – aviad