Wednesday, 22 August 2012

Avoiding Pojomatic Circular References

Pojomatic navigates through object references to perform its duties. Unfortunately, this triggers stack overflow errors with circular references. In order to avoid this, one can exclude fields from processing.

For example:
@AutoPropert
public class CircularReference {

    private String data;

    @Property(policy=PojomaticPolicy.NONE)
    private CircularReference circularReference;

    // Setter, Getters, Constructors & Pojomatic...

}
The following:
CircularReference cr1 = new CircularReference();
cr1.setData("RRR");
cr1.setCircularReference(cr1);

System.out.println(cr1);
Generates:
CircularReference{data: {RRR}}