Я пытаюсь сохранить объект, который имеет коллекцию объектов, например, Invoice с коллекцией InvoiceItems.Я получил JSON с Invoice и InvoiceItems, но после сохранения только Invoice находится в базе данных InvoiceItems не сохраняются.
Invoice
package com.example.demo.entity;
import javax.persistence.*;
import java.util.Collection;
import java.util.Date;
import java.util.Objects;
@Entity
@Table(name = "gp_invoice")
public class GpInvoice {
private int mGpInvoiceRecid;
private String mGpInvoiceTitle;
private String mGpInvoiceNumber;
private Date mGpInvoiceRelease;
private Date mGpInvoicePosting;
private Date mGpInvoiceDelivery;
private String mGpInvoiceReleaseplace;
private GpContractor mGpContractorByGpInvoiceContractor;
private Collection<GpInvoiceitem> mGpInvoiceitemsByGpInvoiceRecid;
@Id
@Column(name = "GP_INVOICE_RECID", nullable = false)
public int getGpInvoiceRecid() {
return mGpInvoiceRecid;
}
public void setGpInvoiceRecid(int gpInvoiceRecid) {
mGpInvoiceRecid = gpInvoiceRecid;
}
@Basic
@Column(name = "GP_INVOICE_TITLE", nullable = false, length = 50)
public String getGpInvoiceTitle() {
return mGpInvoiceTitle;
}
public void setGpInvoiceTitle(String gpInvoiceTitle) {
mGpInvoiceTitle = gpInvoiceTitle;
}
@Basic
@Column(name = "GP_INVOICE_NUMBER", nullable = false, length = 50)
public String getGpInvoiceNumber() {
return mGpInvoiceNumber;
}
public void setGpInvoiceNumber(String gpInvoiceNumber) {
mGpInvoiceNumber = gpInvoiceNumber;
}
@Basic
@Column(name = "GP_INVOICE_RELEASE", nullable = false)
public Date getGpInvoiceRelease() {
return mGpInvoiceRelease;
}
public void setGpInvoiceRelease(Date gpInvoiceRelease) {
mGpInvoiceRelease = gpInvoiceRelease;
}
@Basic
@Column(name = "GP_INVOICE_POSTING", nullable = true)
public Date getGpInvoicePosting() {
return mGpInvoicePosting;
}
public void setGpInvoicePosting(Date gpInvoicePosting) {
mGpInvoicePosting = gpInvoicePosting;
}
@Basic
@Column(name = "GP_INVOICE_DELIVERY", nullable = false)
public Date getGpInvoiceDelivery() {
return mGpInvoiceDelivery;
}
public void setGpInvoiceDelivery(Date gpInvoiceDelivery) {
mGpInvoiceDelivery = gpInvoiceDelivery;
}
@Basic
@Column(name = "GP_INVOICE_RELEASEPLACE", nullable = false, length = 50)
public String getGpInvoiceReleaseplace() {
return mGpInvoiceReleaseplace;
}
public void setGpInvoiceReleaseplace(String gpInvoiceReleaseplace) {
mGpInvoiceReleaseplace = gpInvoiceReleaseplace;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GpInvoice gpInvoice = (GpInvoice) o;
return mGpInvoiceRecid == gpInvoice.mGpInvoiceRecid &&
Objects.equals(mGpInvoiceTitle, gpInvoice.mGpInvoiceTitle) &&
Objects.equals(mGpInvoiceNumber, gpInvoice.mGpInvoiceNumber) &&
Objects.equals(mGpInvoiceRelease, gpInvoice.mGpInvoiceRelease) &&
Objects.equals(mGpInvoicePosting, gpInvoice.mGpInvoicePosting) &&
Objects.equals(mGpInvoiceDelivery, gpInvoice.mGpInvoiceDelivery) &&
Objects.equals(mGpInvoiceReleaseplace, gpInvoice.mGpInvoiceReleaseplace);
}
@Override
public int hashCode() {
return Objects.hash(mGpInvoiceRecid, mGpInvoiceTitle, mGpInvoiceNumber, mGpInvoiceRelease, mGpInvoicePosting, mGpInvoiceDelivery, mGpInvoiceReleaseplace);
}
@ManyToOne
@JoinColumn(name = "GP_INVOICE_CONTRACTOR", referencedColumnName = "GP_CONTRACTOR_RECID", nullable = false)
public GpContractor getGpContractorByGpInvoiceContractor() {
return mGpContractorByGpInvoiceContractor;
}
public void setGpContractorByGpInvoiceContractor(GpContractor gpContractorByGpInvoiceContractor) {
mGpContractorByGpInvoiceContractor = gpContractorByGpInvoiceContractor;
}
@OneToMany(mappedBy = "gpInvoiceByGpInvitInvoicerecid")
public Collection<GpInvoiceitem> getGpInvoiceitemsByGpInvoiceRecid() {
return mGpInvoiceitemsByGpInvoiceRecid;
}
public void setGpInvoiceitemsByGpInvoiceRecid(Collection<GpInvoiceitem> gpInvoiceitemsByGpInvoiceRecid) {
mGpInvoiceitemsByGpInvoiceRecid = gpInvoiceitemsByGpInvoiceRecid;
}
}
InvoiceItem
package com.example.demo.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "gp_invoiceitem")
public class GpInvoiceitem {
private int mGpInvitRecid;
private String mGpInvitName;
private String mGpInvitDescription;
private double mGpInvitPricenet;
private int mGpInvitAmount;
private String mGpInvitUnit;
private int mGpInvitVat;
private double mGpInvitPriceamountnet;
private double mGpInvitPricevat;
private double mGpInvitPricegross;
private GpInvoice mGpInvoiceByGpInvitInvoicerecid;
private byte mGpInvit50;
private String mGpInvitReason;
@Id
@Column(name = "GP_INVIT_RECID", nullable = false)
public int getGpInvitRecid() {
return mGpInvitRecid;
}
public void setGpInvitRecid(int gpInvitRecid) {
mGpInvitRecid = gpInvitRecid;
}
@Basic
@Column(name = "GP_INVIT_NAME", nullable = false, length = 50)
public String getGpInvitName() {
return mGpInvitName;
}
public void setGpInvitName(String gpInvitName) {
mGpInvitName = gpInvitName;
}
@Basic
@Column(name = "GP_INVIT_DESCRIPTION", nullable = true, length = 200)
public String getGpInvitDescription() {
return mGpInvitDescription;
}
public void setGpInvitDescription(String gpInvitDescription) {
mGpInvitDescription = gpInvitDescription;
}
@Basic
@Column(name = "GP_INVIT_PRICENET", nullable = false, precision = 0)
public double getGpInvitPricenet() {
return mGpInvitPricenet;
}
public void setGpInvitPricenet(double gpInvitPricenet) {
mGpInvitPricenet = gpInvitPricenet;
}
@Basic
@Column(name = "GP_INVIT_AMOUNT", nullable = false)
public int getGpInvitAmount() {
return mGpInvitAmount;
}
public void setGpInvitAmount(int gpInvitAmount) {
mGpInvitAmount = gpInvitAmount;
}
@Basic
@Column(name = "GP_INVIT_UNIT", nullable = false, length = 10)
public String getGpInvitUnit() {
return mGpInvitUnit;
}
public void setGpInvitUnit(String gpInvitUnit) {
mGpInvitUnit = gpInvitUnit;
}
@Basic
@Column(name = "GP_INVIT_VAT", nullable = false)
public int getGpInvitVat() {
return mGpInvitVat;
}
public void setGpInvitVat(int gpInvitVat) {
mGpInvitVat = gpInvitVat;
}
@Basic
@Column(name = "GP_INVIT_PRICEAMOUNTNET", nullable = false, precision = 0)
public double getGpInvitPriceamountnet() {
return mGpInvitPriceamountnet;
}
public void setGpInvitPriceamountnet(double gpInvitPriceamountnet) {
mGpInvitPriceamountnet = gpInvitPriceamountnet;
}
@Basic
@Column(name = "GP_INVIT_PRICEVAT", nullable = false, precision = 0)
public double getGpInvitPricevat() {
return mGpInvitPricevat;
}
public void setGpInvitPricevat(double gpInvitPricevat) {
mGpInvitPricevat = gpInvitPricevat;
}
@Basic
@Column(name = "GP_INVIT_PRICEGROSS", nullable = false, precision = 0)
public double getGpInvitPricegross() {
return mGpInvitPricegross;
}
public void setGpInvitPricegross(double gpInvitPricegross) {
mGpInvitPricegross = gpInvitPricegross;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GpInvoiceitem that = (GpInvoiceitem) o;
return mGpInvitRecid == that.mGpInvitRecid &&
Double.compare(that.mGpInvitPricenet, mGpInvitPricenet) == 0 &&
mGpInvitAmount == that.mGpInvitAmount &&
mGpInvitVat == that.mGpInvitVat &&
Double.compare(that.mGpInvitPriceamountnet, mGpInvitPriceamountnet) == 0 &&
Double.compare(that.mGpInvitPricevat, mGpInvitPricevat) == 0 &&
Double.compare(that.mGpInvitPricegross, mGpInvitPricegross) == 0 &&
Objects.equals(mGpInvitName, that.mGpInvitName) &&
Objects.equals(mGpInvitDescription, that.mGpInvitDescription) &&
Objects.equals(mGpInvitUnit, that.mGpInvitUnit);
}
@Override
public int hashCode() {
return Objects.hash(mGpInvitRecid, mGpInvitName, mGpInvitDescription, mGpInvitPricenet, mGpInvitAmount, mGpInvitUnit, mGpInvitVat, mGpInvitPriceamountnet, mGpInvitPricevat, mGpInvitPricegross);
}
@ManyToOne
@JoinColumn(name = "GP_INVIT_INVOICERECID", referencedColumnName = "GP_INVOICE_RECID", nullable = false)
@JsonIgnore
public GpInvoice getGpInvoiceByGpInvitInvoicerecid() {
return mGpInvoiceByGpInvitInvoicerecid;
}
public void setGpInvoiceByGpInvitInvoicerecid(GpInvoice gpInvoiceByGpInvitInvoicerecid) {
mGpInvoiceByGpInvitInvoicerecid = gpInvoiceByGpInvitInvoicerecid;
}
@Basic
@Column(name = "GP_INVIT_50", nullable = false)
public byte getGpInvit50() {
return mGpInvit50;
}
public void setGpInvit50(byte gpInvit50) {
mGpInvit50 = gpInvit50;
}
@Basic
@Column(name = "GP_INVIT_REASON", nullable = false, length = 30)
public String getGpInvitReason() {
return mGpInvitReason;
}
public void setGpInvitReason(String gpInvitReason) {
mGpInvitReason = gpInvitReason;
}
}
Invoice repository
import org.springframework.data.repository.Repository;
import java.util.List;
@org.springframework.stereotype.Repository
public interface InvoiceRepository extends Repository<GpInvoice, Long> {
void save(GpInvoice invoice);
}
Контроллер
@PostMapping(path = "/new")
public void createInvoice(@RequestBody() InvoiceDto gpInvoice) {
mInvoiceService.saveAndFlush(gpInvoice.invoice, gpInvoice.invoiceItems);
}
Сервис
@Override
@Transactional
public void saveAndFlush(GpInvoice invoice, List<GpInvoiceitem> gpInvoiceItem) {
invoice.setGpInvoiceitemsByGpInvoiceRecid(gpInvoiceItem);
mInvoiceRepository.save(invoice);
}
Почему InvoiceItems не сохраняются?