1

I have 3 Classes

  • ClassA contains a_id , a_name
  • ClassB contains b_id , b_name
  • ClassC contains foreign keys of ClassA and ClassB ie,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}]
1
  • 1
    You should try before posting question, and put your efforts and errors then others can help you. Commented Jul 20, 2017 at 6:50

1 Answer 1

2

I Just solved the issue

public Set<ClassCTreeDto> unique() {

            long parent = 0;
            long div_id = 10;

            List<ClassCTreeDto> treestructure = new ArrayList<ClassCTreeDto>();
            List<ClassCTreeDto> treestructure2 = new ArrayList<ClassCTreeDto>();
            Set<ClassCTreeDto> all = new HashSet<ClassCTreeDto>();

            List<ClassC> list = getAll();

            for (ClassC ClassC: list) {
                ClassCTreeDto tree = new ClassCTreeDto();

                tree.setParent(parent);
                tree.setId(ClassC.getFkId().getId());
                tree.setName(ClassC.getFkId().getIdName());

                if (containsLocation(treestructure, tree.getIdName())) {
                    treestructure.remove(tree);

                }

                else {
                    treestructure.add(tree);
                }

            }
            for (ClassC classC : list) {

                ClassCTreeDto tree2 = new ClassCTreeDto ();

                tree2.setParent(classC.getFkId().getId());
                tree2.setId(div_id);
                tree2.setName(classC.getFkDId().getDName());

                treestructure2.add(tree2);

            }

            all.addAll(treestructure);
            all.addAll(treestructure2);

            return all;
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.