0

I have a tree with some level. how to save it in ActiveAndroid ORM? I want to save it but just saved the first layer with a null child. All child objects have the same property.

[
  {
    "childs": [
      {
        "childs": [
          {
            "childs": [
              {
                "childs": null,
                "id": 35,
                "parentId": 11,
                "listLevel": 3
              },
              {
                "childs": null,
                "id": 36,
                "parentId": 11,
                "listLevel": 3
              }
            ],
            "id": 11,
            "profileId": null,
            "parentId": 5,
            "listLevel": 2
          },
          {
            "childs": [
              {
                "childs": null,
                "id": 40,
                "parentId": 12,
                "listLevel": 3
              }
            ]
          }
        ]
      }
    ]
  }
]

its the model of json

@Table(name = "ListItemTable") public class ListItemTable extends Model {

@Column(name = "myId")
public Long myId;
@Column(name = "childs")
public List<ListItemTable> childs;
@Column(name = "listLevel")
public int listLevel;
@Column(name = "parentId")
public int parentId;
public ListItemTable() {
    super();
}

}

  • Can you please share a valid JSON so that we can help you find a solution. Also, please format the JSON so that it is easily readable. – Mohit Ajwani Dec 18 '18 at 07:11
  • this is not a valid JSON formate please share in the proper format. – bugfreerammohan Dec 18 '18 at 07:15
  • This answer will help you better understand structures. Adjacency List method will be easy to implement. The only problem that you might face is it is expensive to find the level, ancestry & descendants, path using this method. Please read through for better understanding. https://stackoverflow.com/questions/4048151/what-are-the-options-for-storing-hierarchical-data-in-a-relational-database – Mohit Ajwani Dec 18 '18 at 09:45
  • There are two approaches: Adjacency list: In the adjacency list model, each item in the table contains a pointer to its parent. The problem with the adjacency model is that the corresponding SQL works on one level at a time, so if you have 'n' levels then you need a loop of 'n' SQL statements. Nested Set: In the Nested Set Model, we can look at our hierarchy in a new way, not as nodes and lines, but as nested containers. I would recommend the Nested set model, as it allows you to get the whole tree in one query – Mohit Ajwani Dec 18 '18 at 09:48
  • its the model of json @Table(name = "ListItemTable") public class ListItemTable extends Model { @Column(name = "myId") public Long myId; @Column(name = "childs") public List childs; @Column(name = "listLevel") public int listLevel; @Column(name = "parentId") public int parentId; public ListItemTable() { super(); } } – Aris Zandi Dec 19 '18 at 12:16

0 Answers0