I have 3 Classes
- ClassA contains
a_id , a_name - ClassB contains
b_id , b_name - ClassC contains foreign keys of
ClassAandClassBie,c_id , c_name , fk_a_id , fk_b_id
i want to create a tree from ClassC as
a_name1 //.....parent
- b_name1
- b_name2
a_name2
- b_name1 //...... childs
- b_name2
i want the json as
[
{
"id":a_id1,
"name":"a_name1",
"parent":0
},
{
"id":b_id1",
"name":"b_name1",
"parent":a_id1
},
{
"id":b_id1,
"name":"b_name2",
"parent":a_id1
},
{
"id":a_id2,
"name":"a_name2",
"parent":0
},
{
"id":b_id1,
"name":"b_name1",
"parent":a_id2
},
{
"id":b_id1,
"name":"b_name2",
"parent":a_id2
}
]
How can i achieve the above json using java and hibernate
My Research
public List<ClassCTreeDto> unique() {
Session session= getSession();
Criteria crit = session.createCriteria(ClassC.class);
List<ClassCTreeDto> hierarchydto=new ArrayList<ClassCTreeDto>();
List<ClassC> cmList = crit.list();
for(ClassCs :cmList){
ClassCTreeDto tDto= new ClassCTreeDto();
tDto.setId(s.getFkId().getId());
tDto.setName(s.getFkId().getIdName());
tDto.setParent(s.getFkId().getGradeId());
if(s.getFkId()==null)
{
tDto.setId((long) 0);
}
else
{
tDto.setId(s.getFkId().getGradeId());
}
hierarchydto.add(tDto);
}
return hierarchydto;
}
but iam getting json as,
[{"id":1,"name":"X","parent":1},
{"id":1,"name":"X","parent":1},
{"id":2,"name":"IX","parent":2},
{"id":2,"name":"IX","parent":2}]