rawkfist0215
rawkfist0215

Reputation: 1485

Deserialization and multiple back-reference properties with name 'defaultReference'

I am trying to deserialize a nested JSON object into POJOs using Jackson 1.9, but I am running into troubles. Below are the classes along with the example JSON string I am trying to parse.

(Getters and setters omitted)

JSON String:

sellerJson = "[{\"id\":\"1\",\"first_name\":\"Joe\",\"last_name\":\"Sellerman\",\"company\":\"NY CANYON RANCH\"," +
                    "\"prorated_sellers\":[{\"first_name\":\"Steve\",\"last_name\":\"Jobs\",\"company\":\"NY CANYON RANCH\"}," +
                    "{\"first_name\":\"Lorne\",\"last_name\":\"Michaels\",\"company\":\"NY CANYON RANCH\"}]," +
                    "\"pens\":[{\"id\":\"2\",\"pen_no\":\"902\"}]}]";

Java classes:

@Table(name="seller")
public class SellerModel implements Serializable, Comparable<SellerModel> {

    private static final long serialVersionUID = 201302111531L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")  
    private Integer id;

    @Column()
    private String first_name;

    @Column()
    private String last_name;

    @Column()
    private String company;

    @Column()
    private Boolean is_prorated;

    @JsonIgnore
    @ManyToOne()
    @JoinColumn(name="parent_seller_id", referencedColumnName="id")
    private SellerModel parent_seller;

    @JsonManagedReference(value="seller-prorated")
    @OneToMany(mappedBy = "parent_seller", cascade={CascadeType.ALL}, orphanRemoval=true)
    private List<SellerModel> prorated_sellers;

    @JsonManagedReference(value="seller-pen")
    @OneToMany(mappedBy = "parent_seller", cascade={CascadeType.ALL}, orphanRemoval=true)
    private List<PenModel> pens;
}

@Table(name="pen")
public class PenModel implements Serializable, Comparable<PenModel> {

    private static final long serialVersionUID = 201302111537L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")  
    private Integer id;

    @Column()
    private String pen_no;

    @JsonBackReference(value="pen-seller")
    @ManyToOne()
    @JoinColumn(name="parent_seller_id", referencedColumnName="id")
    private SellerModel parent_seller;

    @JsonManagedReference(value="pen-livestock")
    @OneToMany(mappedBy = "parent_pen", cascade={CascadeType.ALL}, orphanRemoval=true)
    private List<LivestockModel> livestock;
}

However when I try to perform the deserialization:

List<SellerModel> sellers = (List<SellerModel>) mapper.readValue(sellerJson, 
                    mapper.getTypeFactory().constructCollectionType(List.class, SellerModel.class));

I receive the following exception message:

java.lang.IllegalArgumentException: Multiple back-reference properties with name 'defaultReference'

I read that providing the (value='something-unique') annotation attribute should resolve this, but I am not having any luck. Does anyone have any suggestions?

Upvotes: 5

Views: 7127

Answers (1)

Dimitri Hautot
Dimitri Hautot

Reputation: 458

I see at least two problems. First:

  • SellerModel.prorated_sellers annotated with @JsonManagedReference(value="seller-pen")
  • PenModel.parent_seller annotated with @JsonBackReference(value="pen-seller")

They must have the same name, to be related.

Second:

  • SellerModel.parent_seller should be annotated with @JsonManagedReference(value="seller-prorated") (Managed), and remove the @JsonIgnore annotation
  • SellerModel.prorated_sellers should be annotated with @JsonBackReference(value="seller-prorated") (Back)

Upvotes: 2

Related Questions