title: Get child hierarchy path till parent in java
body: <p>I have a scenario to get a child hierarchy structure of a field till parent for doing field level validations.
  Can someone provide some solution.</p>
  
  <p><strong>Pojo classes</strong></p>
  
  <p><strong>Student.java</strong></p>
  
  <pre><code> package com.poc.next.validations;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class Student {
      private String studentName;
      private List&lt;Subject&gt; subjects;
  
      public String getStudentName() {
          return studentName;
      }
  
      public void setStudentName(String studentName) {
          this.studentName = studentName;
      }
  
      public List&lt;Subject&gt; getSubjects() {
          if (subjects == null) {
              return new ArrayList&lt;&gt;();
          }
          return subjects;
      }
  
      public void setSubjects(List&lt;Subject&gt; subjects) {
          this.subjects = subjects;
      }
  }
  </code></pre>
  
  <p><strong>Subject.java</strong></p>
  
  <pre><code>package com.poc.next.validations;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class Subject {
  
      private String subjectName;
      private List&lt;RevisionMarks&gt; revisionMarks;
  
      public String getSubjectName() {
          return subjectName;
      }
  
      public void setSubjectName(String subjectName) {
          this.subjectName = subjectName;
      }
  
      public List&lt;RevisionMarks&gt; getRevisionMarks() {
          if (revisionMarks == null) {
              return new ArrayList&lt;&gt;();
          }
          return revisionMarks;
      }
  
      public void setRevisionMarks(List&lt;RevisionMarks&gt; revisionMarks) {
          this.revisionMarks = revisionMarks;
      }
  }
  </code></pre>
  
  <p><strong>RevisionMarks.java</strong></p>
  
  <pre><code>package com.poc.next.validations;
  
  public class RevisionMarks {
  
      private Integer mark;
      private String revision;
  
      public Integer getMark() {
          return mark;
      }
  
      public void setMark(Integer mark) {
          this.mark = mark;
      }
  
      public String getRevision() {
          return revision;
      }
  
      public void setRevision(String revision) {
          this.revision = revision;
      }
  }
  </code></pre>
  
  <p>Now we are adding a validation to check whether the given mark in RevisionMarks class in valid or not. if it is equal to zero I have to add it to error dto and send it back to UI. The challenge here is i have to provide the field name dynamic in hierarchy like "subjects[0].revisionMarks[0].mark". </p>
  
  <p><strong>Main class</strong>
  <strong>RevisionValidation.java</strong></p>
  
  <pre><code>package com.poc.next.validations;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  public class RevisionValidation {
  
      public static void main(String[] args) {
          Student student = populateStudentData();
          Iterator&lt;Subject&gt; iterator = student.getSubjects().iterator();
          while (iterator.hasNext()) {
              Subject subject = (Subject) iterator.next();
              RevisionMarks revisionMarks = subject.getRevisionMarks().get(0);
              System.out.println(revisionMarks.getMark());
              if (revisionMarks.getMark() == 0) {
                  ErrorDTO errorDTO = new ErrorDTO(true, "Invalid Marks", "Error", "subjects[0].revisionMarks[0].mark",
                          "invalid_mark");
                  System.out.println(errorDTO);
              }
          }
      }
  
      private static Student populateStudentData() {
          List&lt;RevisionMarks&gt; revisionMarks = new ArrayList&lt;&gt;();
          RevisionMarks revisionMark = new RevisionMarks();
          revisionMark.setMark(0);
          revisionMark.setRevision("Test 1");
          revisionMarks.add(revisionMark);
  
          List&lt;Subject&gt; subjects = new ArrayList&lt;&gt;();
          Subject subject = new Subject();
          subject.setSubjectName("CS");
          subject.setRevisionMarks(revisionMarks);
          subjects.add(subject);
  
          Student student = new Student();
          student.setStudentName("Sample");
          student.setSubjects(subjects);
          return student;
      }
  
  }
  </code></pre>
  
  <p>How can I dynamically create the fieldpath like "subjects[0].revisionMarks[0].mark". 
  Any suggestions are welcome. Thanks in advance.</p>