Pergunta

I have a parent child hierarchy which I am storing in single table by maintaining self foreign key. My table columns are ID, Name, ParentID. Here, ParentID is referencing ID column of same table.

And my model class is like,

Class Model
{
    private int id;
    private String name;
    private Model parent;
    private List<Model> children;
}

How can I use RowMapper to map data in this model? Or, what is the best way to achieve this?

I am retrieving entire hierarchy in single query.

Foi útil?

Solução

Creatre a field in your RowMapper - Map. When you parse result place each the Model items also in the map where key is the item's id.

To add a child to appropriate parent read the parent Model element from the map by parent id of curretn item.

2 things to mention

Root element has no parent. Your query should provide correct order (parent must be processed first)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top