Search code examples
javajsonhibernatemarshalling

Marshall a hibernate pojo class with OnetoMany relation


I have hibernate pojo class which has a ManytoOne relation with another class.

 class Employee {        
    @OneToMany
       private String id; 
   }

class ITEmployee {
 private Employee employee;

 @ManyToOne
 @JoinColumn(name="EMPLOYEE_ID)
  public Emplyee getEmployee() {
    return employee;
    }

}

Now when I retrieve a row and marshall to a JSON/XML REST response, I get nested object of Employee class and ITEmployee class within each object.

Like eg,

{"ITEmployee":[{"id":1234,"Employee":[{"id":222, "ITEmployee":{"id":1234,"Employee":[{"id":222, "Employee":[{"id":222, "ITEmployee": . .. .

and so on.

How can I ignore the ManytoOne relation while marshalling?

I don't want to create another class and map them seperately.

I tried using @JsonIgnore and @Transient but that didn't work.

REST API : JAX-RS Cheers!!


Solution

  • You can tell Jackson to not marshall some fields. You have multiple choices. The simpliest is to use @JsonIgnore annotation on your employee Field.

    If you want more advanced features, you can check for @JsonView.

    EDIT : I see you already tried to use @JsonIgnore. Can you paste your code ? In principe it must work.