mercredi 5 août 2015

java.lang.IllegalArgumentException: Can not handle managed/back reference


I am having the following entity relationship

@Entity
@Table(name = "PRODUCTS")
public class Product implements Serializable {

@Id
@Column(name = "prod_id", nullable = false)
private String prodId;

@Column(name = "prod_name")
private String prodName;

@JsonManagedReference("prods")
@ManyToOne
@JoinColumn(name = "prod_id", referencedColumnName = "prod_id")
private ProductDetail productDetail;

@Entity
@Table(name = "PRODUCT_DETAILS")
public class ProductDetail implements Serializable {

@Id
@Column(name="prod_id", nullable = false)
private String prodId;

@JsonBackReference("prods")
@OneToMany(mappedBy = "productDetail")
private List<Product> productList;

And in REST service, I have

@PUT
@Produces(MediaType.APPLICATION_JSON)
@Path("/mypath")
public Object update(Product product)  {
    System.out.println("params "+product.getProdName());
    return json;
}   

When I invoke update method using REST, I am getting error

java.lang.IllegalArgumentException: Can not handle managed/back reference 'prods': back reference type (java.util.List) not compatible with managed type (test.entity.Product)

How can I resolve this issue?

Edit 1

I have tried using

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,
property="prodId")

However this resulted in the following error

org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain:



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire