Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

java - If two entity parent and child saved at same time which mapped with one to many relationship throw id not found exception of parent class

My relationship like this which is shown in image : enter image description here

I'm having troubling with insert data in database because of bill id saved little bit late and that time also buybill save simultaneously in database and which want to bill id to save in relationship and throw exception like invoice id not found and only my bill data saved in database and buybill thrwo exception of not found bill id so please help me out.

Thanks,

And one more thing that i include that my postmapping of :

This--->

@PostMapping(value="/customers/{customerId}/bills/{InvoiceNo}/buyBills",produces="application/json",consumes="application/json")

and

This--->

@PostMapping(value="/customers/{customerId}/bills/{InvoiceNo}/bills",produces="application/json",consumes="application/json")

by two $http.post() simultaneously with one angularjs submit button in html.

This is my javascript code of post two http request by angular.js onclick of $scope.purchase function where there i post two url which mapped above URL1 and URL2 with json data and BillData, buyBillFormss.html code :

<script type="text/javascript">
var invoice = angular.module('invoice', []);
invoice.controller('InvoiceController', function($scope,$window,$http){

$scope.purchase = function(){
    if(!$scope.myForm.$valid){
        console.log("Invalid")
        $scope.err = "Invaid Transaction Please Insert valid field or Refresh!!!";
        }
    if($scope.myForm.$valid){
        angular.forEach($scope.invoice.items, function(item){
            var Bill = []; 
            $scope.am = (((item.qty * item.price)+((item.qty * item.price)*item.gst)/100)-item.dis).toFixed(2);
            angular.forEach($scope.invoice.items, function (value, key) {
                var am = (((value.qty * value.price)+((value.qty * value.price)*value.gst)/100)-value.dis).toFixed(2);
                Bill.push({
                    "proId" : value.proId,
                     "name" : value.name,
                     "description" : value.description,
                     "qty" : value.qty,
                     "unit" : value.unit,
                     "price" : value.price,
                     "dis" : value.dis,
                     "gst" : value.gst,
                     "amount" : am
                     });
                });
                console.log("Bill ::");
                console.log(Bill);
                localStorage.setItem("data",JSON.stringify(Bill));
                ///////////////////////////////////////////////////////
                var id = document.getElementById("ids").innerText;
                var InvoiceNo = document.getElementById("InvoiceNo").innerText;
                var data={
                        "proId" : item.proId,
                        "name" : item.name,
                        "description" : item.description,
                        "qty" : item.qty,
                        "unit" : item.unit,
                        "price" : item.price,
                        "dis" : item.dis,
                        "gst" : item.gst,
                        "amount" : $scope.am
                        };
                console.log("Data ::");
                console.log(data);
                $scope.CustomerId = id;
                $scope.InvoiceNo  = InvoiceNo;
                var URL1 = "http://localhost:8083/cust/customers/"+$scope.CustomerId+"/bills/"+$scope.InvoiceNo+"/buyBills";
                $http.post(URL1, data);
                
        });
        //angular.forEach($scope.invoice.items, function(item){
            var id = document.getElementById("ids").innerText;
            var name = document.getElementById("name").innerText;
            var InvoiceNo = document.getElementById("InvoiceNo").innerText;
            var address = document.getElementById("address").innerText;
            var mobileNo = document.getElementById("mobileNo").innerText;
            var note = document.getElementById("n").value;
            var InterestRate = document.getElementById("i").value;
            var CredibilityStatus = "very Good";
            var guarantorName = document.getElementById("g").value;
            var BillData={
                    "invoiceNo" : InvoiceNo,
                     "name" : name,
                     "address" : address,
                     "mobileNo" : mobileNo,
                     "totalGSTAmount" : ($scope.GST()).toFixed(2),
                     "totalDiscountAmount" : $scope.Dis(),
                     "guarantorName" : guarantorName,
                     "totalAmount" : ($scope.TotalAmount()).toFixed(2),
                     "paidAmount" :  ($scope.PaidAmount()).toFixed(2),
                     "dueAmount" :  ($scope.DueAmount()).toFixed(2),
                     "status" : $scope.Status(),
                     "interestRate" : InterestRate,
                     "credibilityStatus" : CredibilityStatus, 
                     "note"   : note
                     };
            console.log("BillData ::");
            console.log(BillData);
            $scope.CustomerId = id;
            $scope.InvoiceNo  = InvoiceNo;
            var URL2 = "http://localhost:8083/cust/customers/"+$scope.CustomerId+"/bills/"+$scope.InvoiceNo+"/bills";
            $http.post(URL2, BillData);
            localStorage.setItem("dataAct",JSON.stringify(BillData));
            //});
        $window.location.href = "/Bill"
        
        }
}   
});
</script>

My code is here :

This is my customer.java entity :

package com.alpha.demo.model;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.CreationTimestamp;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "customers")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    
    @Column(name = "uniqueId", updatable = false, nullable = false)
    private UUID uniqueId = UUID.randomUUID();
    @Column(columnDefinition = "TEXT")
    private String photos;
    private String fullName;
    private String aadhaarNo;
    private String guarantor;
    private String address;
    private String mobileNo;
    private String note;
    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_date",updatable=false)
    private Date createDate;
    @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 
    private Set<Bill> Bill;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public UUID getUniqueId() {
        return uniqueId;
    }
    public void setUniqueId(UUID uniqueId) {
        this.uniqueId = uniqueId;
    }
    public String getFullName() {
        return fullName;
    }
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    
    public String getPhotos() {
        return photos;
    }
    public void setPhotos(String photos) {
        this.photos = photos;
    }
    public String getAadhaarNo() {
        return aadhaarNo;
    }
    public void setAadhaarNo(String aadhaarNo) {
        this.aadhaarNo = aadhaarNo;
    }
    public String getGuarantor() {
        return guarantor;
    }
    public void setGuarantor(String guarantor) {
        this.guarantor = guarantor;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getMobileNo() {
        return mobileNo;
    }
    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }
    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }
    
    public Set<Bill> getBill() {
        return Bill;
    }
    public void setBill(Set<Bill> bill) {
        Bill = bill;
    }
    public Customer(String photos, String fullName, String aadhaarNo, String guarantor, String address, String mobileNo,
            String note, Date createDate) {
        super();
        this.photos = photos;
        this.fullName = fullName;
        this.aadhaarNo = aadhaarNo;
        this.guarantor = guarantor;
        this.address = address;
        this.mobileNo = mobileNo;
        this.note = note;
        this.createDate = createDate;
    }
    public Customer() {
    }

}

This is my Bill.java entity :

package com.alpha.demo.model;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.CreationTimestamp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "bills")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) 
public class Bill implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long invoiceNo;
    private String guarantorName;
    private String TotalGSTAmount;
    private String TotalDiscountAmount;
    private String TotalAmount;
    private String PaidAmount;
    private String DueAmount;
    private String InterestRate;
    private String TotalInterestAmount;
    private String Status;
    private String Credibility

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I notice that in Bill you have @generatedValue on invoiceId. I don't think you can use @generatedValue on a non @Id field. See this post.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...