我正在使用JPA和MySql,我创建了3个表,它们之间的关系为@OneToMany和@ManyToOne。我试图从表中描述关系的记录中获取数据。尝试获取数据后,我在JSON数据的第1行第183719列收到此错误SyntaxError:JSON.parse:未终止的字符串文字。我只是从gp_contractor表中看到很多记录。当我使用@JsonIgnore与gp_contractor关联时,我可以从表gp_invoice和gp_invoiceitem中获取所有数据。我不知道怎么了。
@Entity
@Table(name = "gp_contractor")
public class GpContractor {
private int mGpContractorRecid;
private String mGpContractorName;
private String mGpContractorNip;
private String mGpContractorStreet;
private String mGpContractorHouseNumber;
private String mGpContractorFlatNumber;
private String mGpContractorZipCode;
private String mGpContractorCity;
private Collection<GpInvoice> mGpInvoicesByGpContractorRecid;
@Id
@Column(name = "GP_CONTRACTOR_RECID", nullable = false)
public int getGpContractorRecid() {
return mGpContractorRecid;
}
public void setGpContractorRecid(int gpContractorRecid) {
mGpContractorRecid = gpContractorRecid;
}
@Basic
@Column(name = "GP_CONTRACTOR_NAME", nullable = false, length = 50)
public String getGpContractorName() {
return mGpContractorName;
}
public void setGpContractorName(String gpContractorName) {
mGpContractorName = gpContractorName;
}
@Basic
@Column(name = "GP_CONTRACTOR_NIP", nullable = false, length = 10)
public String getGpContractorNip() {
return mGpContractorNip;
}
public void setGpContractorNip(String gpContractorNip) {
mGpContractorNip = gpContractorNip;
}
@Basic
@Column(name = "GP_CONTRACTOR_STREET", nullable = false, length = 50)
public String getGpContractorStreet() {
return mGpContractorStreet;
}
public void setGpContractorStreet(String gpContractorStreet) {
mGpContractorStreet = gpContractorStreet;
}
@Basic
@Column(name = "GP_CONTRACTOR_HOUSE_NUMBER", nullable = false, length = 10)
public String getGpContractorHouseNumber() {
return mGpContractorHouseNumber;
}
public void setGpContractorHouseNumber(String gpContractorHouseNumber) {
mGpContractorHouseNumber = gpContractorHouseNumber;
}
@Basic
@Column(name = "GP_CONTRACTOR_FLAT_NUMBER", nullable = true, length = 10)
public String getGpContractorFlatNumber() {
return mGpContractorFlatNumber;
}
public void setGpContractorFlatNumber(String gpContractorFlatNumber) {
mGpContractorFlatNumber = gpContractorFlatNumber;
}
@Basic
@Column(name = "GP_CONTRACTOR_ZIP_CODE", nullable = false, length = 10)
public String getGpContractorZipCode() {
return mGpContractorZipCode;
}
public void setGpContractorZipCode(String gpContractorZipCode) {
mGpContractorZipCode = gpContractorZipCode;
}
@Basic
@Column(name = "GP_CONTRACTOR_CITY", nullable = false, length = 50)
public String getGpContractorCity() {
return mGpContractorCity;
}
public void setGpContractorCity(String gpContractorCity) {
mGpContractorCity = gpContractorCity;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GpContractor that = (GpContractor) o;
return mGpContractorRecid == that.mGpContractorRecid &&
Objects.equals(mGpContractorName, that.mGpContractorName) &&
Objects.equals(mGpContractorNip, that.mGpContractorNip) &&
Objects.equals(mGpContractorStreet, that.mGpContractorStreet) &&
Objects.equals(mGpContractorHouseNumber, that.mGpContractorHouseNumber) &&
Objects.equals(mGpContractorFlatNumber, that.mGpContractorFlatNumber) &&
Objects.equals(mGpContractorZipCode, that.mGpContractorZipCode) &&
Objects.equals(mGpContractorCity, that.mGpContractorCity);
}
@Override
public int hashCode() {
return Objects.hash(mGpContractorRecid, mGpContractorName, mGpContractorNip, mGpContractorStreet, mGpContractorHouseNumber, mGpContractorFlatNumber, mGpContractorZipCode, mGpContractorCity);
}
@OneToMany(mappedBy = "gpContractorByGpInvoiceContractor")
public Collection<GpInvoice> getGpInvoicesByGpContractorRecid() {
return mGpInvoicesByGpContractorRecid;
}
public void setGpInvoicesByGpContractorRecid(Collection<GpInvoice> gpInvoicesByGpContractorRecid) {
mGpInvoicesByGpContractorRecid = gpInvoicesByGpContractorRecid;
}
}
@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;
}
}
@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;
@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)
public GpInvoice getGpInvoiceByGpInvitInvoicerecid() {
return mGpInvoiceByGpInvitInvoicerecid;
}
public void setGpInvoiceByGpInvitInvoicerecid(GpInvoice gpInvoiceByGpInvitInvoicerecid) {
mGpInvoiceByGpInvitInvoicerecid = gpInvoiceByGpInvitInvoicerecid;
}
}
public interface InvoiceRepository extends Repository<GpInvoice, Integer> {
List<GpInvoice> findAll();
GpInvoice findByGpInvoiceRecid(long id);
void delete(GpInvoice gpInvoice);
}
@Service
public class InvoiceServiceImpl implements InvoiceService {
@Autowired
private InvoiceRepository mInvoiceRepository;
@Override
@Transactional
public List<GpInvoice> findAll() {
return mInvoiceRepository.findAll();
}
@Override
@Transactional
public GpInvoice findById(long id) {
return mInvoiceRepository.findByGpInvoiceRecid(id);
}
@Override
@Transactional
public GpInvoice delete(long id) {
GpInvoice gpInvoice = findById(id);
if (gpInvoice != null) {
mInvoiceRepository.delete(gpInvoice);
}
return gpInvoice;
}
}
@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
@RestController
@RequestMapping(value = "/invoice")
public class InvoiceController {
@Autowired
private InvoiceService mInvoiceService;
@GetMapping(path = "/all")
public List findAllInvoices() {
return mInvoiceService.findAll();
}
@DeleteMapping(path = {"/{id}"})
public GpInvoice delete(@PathVariable("id") long id) {
return mInvoiceService.delete(id);
}
}