These 3 methods are to save both parent and child
public Response createMultipleChilds(PurchaseOrderHeaderDto purchaseOrderHeaderDto) {
try {
PurchaseOrderHeader orderDo = importDao(purchaseOrderHeaderDto);
createParent(orderDo);
for (int i = 0; i < orderDo.getPurchaseItemList().size(); i++) {
orderDo.getPurchaseItemList().get(i).setHeaderDetails(orderDo);
createChild(orderDo.getPurchaseItemList().get(i));
}
response.setStatus(200);
response.setMessage(" both added succesfull");
} catch (Exception e) {
System.err.println("Exception : " + e.getMessage());
response.setStatus(500);
response.setMessage("parent not added");
} finally {
s.close();
}
return response;
}
private void createChild(PurchaseItem purchaseItem) {
try {
s = sf.openSession();
tx = s.beginTransaction();
PurchaseItem item = new PurchaseItem();
if (purchaseItem.getItemName() instanceof String) {
item.setItemName(purchaseItem.getItemName());
}
if (purchaseItem.getItemPrice() instanceof String) {
item.setItemPrice(purchaseItem.getItemPrice());
}
item.setItemCode(purchaseItem.getItemCode());
item.setHeaderDetails(purchaseItem.getHeaderDetails());
s.save(item);
response.setStatus(200);
response.setMessage(" child added succesfull");
if (tx.isActive()) {
tx.commit();
}
} catch (Exception e) {
System.err.println("Exception : " + e.getMessage());
response.setStatus(500);
response.setMessage("child not added");
tx.rollback();
} finally {
s.close();
}
}
private void createParent(PurchaseOrderHeader purchaseOrderHeader) {
try {
s = sf.openSession();
tx = s.beginTransaction();
PurchaseOrderHeader order = new PurchaseOrderHeader();
if (purchaseOrderHeader.getCompany_code() instanceof String) {
order.setCompany_code(purchaseOrderHeader.getCompany_code());
}
if (purchaseOrderHeader.getCurrency() instanceof String) {
order.setCurrency(purchaseOrderHeader.getCurrency());
}
if (purchaseOrderHeader.getStatus() instanceof String) {
order.setStatus(purchaseOrderHeader.getStatus());
}
if (purchaseOrderHeader.getVendor() instanceof String) {
order.setVendor(purchaseOrderHeader.getVendor());
}
if (purchaseOrderHeader.getOrderId() instanceof Integer) {
order.setOrderId(purchaseOrderHeader.getOrderId());
}
if (purchaseOrderHeader.getPurchaseItemList() instanceof List) {
order.setPurchaseItemList(purchaseOrderHeader.getPurchaseItemList());
}
order.setDocument_number(purchaseOrderHeader.getDocument_number());
s.save(order);
response.setStatus(200);
response.setMessage(" parent added succesfull");
if (tx.isActive()) {
tx.commit();
}
} catch (Exception e) {
System.err.println("Exception : " + e.getMessage());
response.setStatus(500);
response.setMessage("parent not added");
tx.rollback();
} finally {
s.close();
}
}
Это 2 класса Do
package com.sample.entity;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.FilterJoinTable;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.ParamDef;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@Entity
@Table(name="PurchaseItem")
public class PurchaseItem{
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(name="itemId")
private int itemId;
@JsonBackReference
@ManyToOne(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "document_number")
private PurchaseOrderHeader headerDetails;
@Column(name="itemName")
private String itemName;
@Column(name="itemPrice")
private String itemPrice;
/*@JsonIgnore(true)*/
@Column(name="itemCode")
private Integer itemCode;
public Integer getItemCode() {
return itemCode;
}
public void setItemCode(Integer itemCode) {
this.itemCode = itemCode;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public PurchaseOrderHeader getHeaderDetails() {
return headerDetails;
}
public void setHeaderDetails(PurchaseOrderHeader headerDetails) {
this.headerDetails = headerDetails;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemPrice() {
return itemPrice;
}
public void setItemPrice(String itemPrice) {
this.itemPrice = itemPrice;
}
public PurchaseItem() {
}
@Override
public String toString() {
return "PurchaseItem [itemId=" + itemId + ", headerDetails=" + headerDetails + ", itemName=" + itemName
+ ", itemPrice=" + itemPrice + "]";
}
}
и:
package com.sample.entity;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.Filters;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.ParamDef;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name="PurchaseOrderHeader")
@FilterDef(name = "join1", parameters = {@ParamDef( name = "itemId", type = "int" ),
@ParamDef( name = "document_number", type = "int" ) })
@FilterDef(name = "orderFilter", parameters = { @ParamDef(name = "document_number", type = "int"),
})
@Filters({
@Filter(name = "orderFilter", condition = "document_number>:document_number"),
@Filter(name = "orderFilter", condition = "status=:status"),
})
public class PurchaseOrderHeader {
@Id
@GenericGenerator(name = "generator", strategy = "increment")
@GeneratedValue(generator = "generator")
@Column(name="document_number", nullable=false)
private int document_number;
/*@JsonIgnore*/
@JsonBackReference
@OneToMany(fetch = FetchType.EAGER, mappedBy ="headerDetails",cascade = CascadeType.ALL)
@Filters({
@Filter(name = "join1", condition = "itemId >= :itemId"),
@Filter(name = "join1", condition = "document_number >= :document_number")
})
private List<PurchaseItem> purchaseItemList;
@Column(name="company_code")
private String company_code;
@Column(name="status")
private String status;
@Column(name="vendor", nullable=false)
private String vendor;
@Column(name="currency", nullable =false)
private String currency;
/* @JsonIgnore*/
@Column(name="orderId")
private Integer orderId;
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public int getDocument_number() {
return document_number;
}
public void setDocument_number(int document_number) {
this.document_number = document_number;
}
public String getCompany_code() {
return company_code;
}
public void setCompany_code(String company_code) {
if(company_code instanceof String)
this.company_code = company_code;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
if(status instanceof String)
this.status = status;
}
public String getVendor() {
return vendor;
}
public void setVendor(String vendor) {
if(vendor instanceof String )
this.vendor = vendor;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
if(currency instanceof String )
this.currency = currency;
}
public List<PurchaseItem> getPurchaseItemList() {
return purchaseItemList;
}
public void setPurchaseItemList(List<PurchaseItem> purchaseItemList) {
if(purchaseItemList instanceof List)
this.purchaseItemList = purchaseItemList;
}
public PurchaseOrderHeader() {
}
}
Это два DTOклассы
package com.sample.DTO;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sample.entity.PurchaseOrderHeader;
public class PurchaseItemDto {
private int itemId;
@ManyToOne(fetch=FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "document_number")
private PurchaseOrderHeader headerDetails;
private String itemName;
private String itemPrice;
private String itemCode;
public String getItemCode() {
return itemCode;
}
@JsonIgnore
public void setItemCode(String itemCode) {
this.itemCode = itemCode;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public PurchaseOrderHeader getHeaderDetails() {
return headerDetails;
}
public void setHeaderDetails(PurchaseOrderHeader headerDetails) {
this.headerDetails = headerDetails;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemPrice() {
return itemPrice;
}
public void setItemPrice(String itemPrice) {
this.itemPrice = itemPrice;
}
public PurchaseItemDto() {
}
}
и
package com.sample.DTO;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import com.sample.entity.PurchaseItem;
public class PurchaseOrderHeaderDto {
private int document_number;
@OneToMany(fetch = FetchType.EAGER, mappedBy ="headerDetails",cascade = CascadeType.ALL)
private List<PurchaseItem> purchaseItemList;
private String company_code;
private String status;
private String vendor;
private String currency;
private String orderId;
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public int getDocument_number() {
return document_number;
}
public void setDocument_number(int document_number) {
this.document_number = document_number;
}
public List<PurchaseItem> getPurchaseItemList() {
return purchaseItemList;
}
public void setPurchaseItemList(List<PurchaseItem> purchaseItemList) {
if(purchaseItemList instanceof List)
this.purchaseItemList = purchaseItemList;
}
public String getCompany_code() {
return company_code;
}
public void setCompany_code(String company_code) {
if(company_code instanceof String )
this.company_code = company_code;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
if(status instanceof String)
this.status = status;
}
public String getVendor() {
return vendor;
}
public void setVendor(String vendor) {
if(vendor instanceof String)
this.vendor = vendor;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
if(currency instanceof String)
this.currency = currency;
}
public PurchaseOrderHeaderDto() {
// TODO Auto-generated constructor stub
}
}
JSON parse error: Cannot deserialize value of type `java.lang.Integer` from String "sagar": not a valid Integer value;
Вложенное исключение: com.fasterxml.jackson.databind.exc.InvalidFormatException: Невозможно десериализовать значение типа java.lang.Integer
изСтрока «sagar»: недопустимое целочисленное значение в [Source: (PushbackInputStream);строка: 19, столбец: 13] (через цепочку ссылок: com.sample.DTO.PurchaseOrderHeaderDto ["purchaseItemList"] -> java.util.ArrayList [2] -> com.sample.entity.PurchaseItem ["itemCode"])]