miklesw
miklesw

Reputation: 734

Spring MVC, Domain Objects & @JsonIgnore

I'm working with Spring MVC 3 and trying to use my domain objects to return a json response. The problem is that due to bi-directional relationships and a self relationship (employee reportsTo employee), I am receiving a JsonMappingException: Infinite recursion

I tried using @JsonIgnore but spring/jackson still attempt to include the attributes ( Infinite Recursion with Jackson JSON and Hibernate JPA issue )

Any idea why @JsonIgnore is not kicking in?

I know that transfer objects or jsonviews are a better way of doing this, but i'de still like to get to the bottom of this before i move on.

Customer.java

  @Entity
@NamedQueries({
    @NamedQuery(name="Customer.findByName", query="from Customer cust where cust.customerName like :customerName"),
    @NamedQuery(name="Customer.findByAccountNumber", query="from Customer cust where cust.accountNumber = :accountNumber"),
    })
public class Customer implements DomainObject {
    private Long id;
    private String accountNumber;
    private String customerName;
    private CustomerDerived derived;
    private Employee salesRep;
    private Set<Order> orders = new HashSet<Order>();

    @Id
    @GeneratedValue
    @Column(name="Id")
    public Long getId() {   return id;  }
    public void setId(Long id) {    this.id=id; }

    @Column(unique=true)
    public String getAccountNumber() {return accountNumber;}
    public void setAccountNumber(String accountNumber) {this.accountNumber = accountNumber; }

    public String getCustomerName() {   return customerName;    }
    public void setCustomerName(String customerName) {  this.customerName = customerName;}

    @JsonIgnore
    @ManyToOne( fetch = FetchType.LAZY)
    @JoinColumn(name="salesRepEmployeeId")
    public Employee getSalesRep() { return salesRep;    }
    public void setSalesRep(Employee salesRep) {this.salesRep = salesRep;}

    @JsonIgnore
    @OneToMany(mappedBy="customer", fetch = FetchType.LAZY)
    public Set<Order> getOrders() { return orders;  }
    public void setOrders(Set<Order> orders) {this.orders = orders; }

    @OneToOne
    @PrimaryKeyJoinColumn(name="customerId")
    public CustomerDerived getDerived() {return derived;}
    public void setDerived(CustomerDerived derived) {this.derived = derived;}

}

TestController.java

@Controller
@RequestMapping(value="/test")
public class TestController {
    private CustomerDao customerDao;

    @Autowired
    public TestController(CustomerDao customerDao){
        this.customerDao=customerDao;
    }

    @RequestMapping(value="/getAll", method=RequestMethod.GET)
    public String getAll(Model model){
        List<Customer> customers = customerDao.findAll();
        model.addAttribute("customers", customers);
        return "testresult";
    }

    @RequestMapping(value="/searchByName/{name}", method=RequestMethod.GET )
     public @ResponseBody List<Customer> search(@PathVariable String name){
        List<Customer> customers = customerDao.findByName(name);
        System.out.println("got customers");
        return customers;
    }
 }

Stacktrace

SEVERE: Servlet.service() for servlet orderWeb threw exception
org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.mike.orderapp.domain.Office["employees"]->org.hibernate.collection.PersistentSet[0]->com.mike.orderapp.domain.Employee_$$_javassist_0["office"]->com.mike.orderapp.domain.Office["employees"]->org.hibernate.collection.PersistentSet[0]->com.mike.orderapp.domain.Employee_$$_javassist_0["office"]->com.mike.orderapp.domain.Office["employees"]->org.hibernate.collection.PersistentSet[0]-
...... 
>com.mike.orderapp.domain.Employee_$$_javassist_0["office"]->com.mike.orderapp.domain.Office["employees"])
    at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:164)
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
    at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)

Upvotes: 2

Views: 4076

Answers (2)

Vishal jotshi
Vishal jotshi

Reputation: 76

use @jsonManagedRefrence and @JsonBackRefrence, my problem got solved by this .

Upvotes: 0

James DW
James DW

Reputation: 1815

Based on your exception it looks like the circular reference is from your Office class which is not listed here.

I would really consider creating those transfer objects. In addition to complicating your JPA model you are coupling your domain to a very specific, non domain, set of classes.

If you wanted to change your JSON parsing implementation you would have to change your domain objects.

Upvotes: 1

Related Questions