Reputation: 159
Owner Entity: PriceListItemTest.java
@JsonPropertyOrder({"pricingUOMCode","lineTypeCode","primaryPricingUOMFlag","priceListId","itemId","priceListItemId"})
@Entity
@Table(name = "QP_PRICE_LIST_ITEMS")
public class PriceListItemTest implements Serializable{
@Column(name = "price_list_item_id")
private String priceListItemId;
@Column(name = "pricing_uom_code")
private String pricingUOMCode;
@Column(name = "line_type_code")
private String lineTypeCode;
@Column(name = "primary_pricing_uom_flag")
private String primaryPricingUOMFlag;
@Id
@Column(name = "item_id")
private String itemId;
@OneToMany(mappedBy = "priceListItemsTest", cascade = CascadeType.ALL)
private List<ItemDetailTest> itemDetailsTest;
// getters and setters
}
and Entity2 : ItemDetailTest.java
@JsonPropertyOrder({"itemNumber","inventoryItemId","organizationId"})
@Entity
@Table(name = "egp_system_items_b")
public class ItemDetailTest implements Serializable {
@Id
@Column(name = "inventory_item_id")
private String inventoryItemId;
@Column(name = "item_number")
private String itemNumber;
@Column(name = "organization_id")
private String organizationId;
// To be used for bidirectional
@ManyToOne//(fetch = FetchType.LAZY)
private PriceListItemTest priceListItemsTest;
// getters and setters
}
The RestController is returning error while retrieving the JSON result.
{
"timestamp": "2018-10-23T14:05:26.248+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Could not write JSON: could not extract ResultSet; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not extract ResultSet (through reference chain: java.util.ArrayList[0]->com.Pricing.Pricing_App.model.PriceListItemTest[\"itemDetailsTest\"])",
"path": "/getByItemIdTest"
}
May be the mapping oneTomany is wrong.
It is working fine if it has oneToone mapping but facing problem with OneToMany.
Can someone please help me with the error?
Edit: Controller:
@RestController
public class PriceListItemControllerTest {
@Autowired
private PriceListItemRepositoryTest priceListItemRepositoryTest;
@GetMapping(value="/getByItemIdTest")
public List<PriceListItemTest> getByItemIdTest(@RequestParam("itemId") String itemId) {
return priceListItemRepositoryTest.findByItemId(itemId);
}
}
Upvotes: 2
Views: 8711
Reputation: 851
use @JsonIgnore like in the below. problem is infinite json calls are going through your json call. to ignore that use this annotation on your ManytoOne relation. worked fine for me.
@ManyToOne//(fetch = FetchType.LAZY)
@JsonIgnore
private PriceListItemTest priceListItemsTest;
Upvotes: 5
Reputation: 2004
Change your model class like below:
@Entity
@Table(name = "QP_PRICE_LIST_ITEMS")
public class PriceListItemTest implements Serializable {
private static final long serialVersionUID = -748956247024967638L;
@Basic
@Column(name = "price_list_item_id")
private String priceListItemId;
@Basic
@Column(name = "pricing_uom_code")
private String pricingUOMCode;
@Basic
@Column(name = "line_type_code")
private String lineTypeCode;
@Basic
@Column(name = "primary_pricing_uom_flag")
private String primaryPricingUOMFlag;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "item_id")
private String itemId;
@JsonManagedReference
@OneToMany(mappedBy = "priceListItemsTest", cascade = CascadeType.ALL)
private List<ItemDetailTest> itemDetailsTest;
// getters and setters
}
and
@Entity
@Table(name = "egp_system_items_b")
public class ItemDetailTest implements Serializable {
private static final long serialVersionUID = 8495817802073010928L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "inventory_item_id")
private String inventoryItemId;
@Basic
@Column(name = "item_number")
private String itemNumber;
@Basic
@Column(name = "organization_id")
private String organizationId;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "fk_item_id", nullable = false) // name of the foreign key column
private PriceListItemTest priceListItemsTest;
// getters and setters
}
In your JoinColumn provide the actual name of the foreign key field. It should work as Jackson will automatically serialize.
For One To many refernces you can follow these links: link1 and link2
Upvotes: 6
Reputation: 36103
You have a bidirectional mapping between the two entities and this would cause an infinit loop during JSON serialization.
You must ignore one of the sides using @JsonIgnore for example.
Read more about that topic here:
https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
Upvotes: 2