2

I am sending form data from angular to spring boot to save in MySQL.

Form has few fields taking text and one field is date (input type=date )=>bootstrap

Bill Model

    export class Bill{
          billno:string
          date:Date
          vendorname:string
          amount:number
          description:string
} 

Angular Service for sending data to spring

onSubmit(){

  this.addBillForm.markAllAsTouched();
  if(this.addBillForm.invalid){
    console.log("Form is Invalid");
    return false;
  }
  this.datasaveService.addbill(this.addBillForm.value)
  .subscribe(
    res=>{
      console.log("Response : "+res);
    });
}



  addbill(bill:Bill):Observable<any>{
    return this.http.post<Bill>(this.addbillurl,bill);
  }

Bill Entity Class :

@Entity
@Table(name="bill_details") 
public class Bills {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="bill_id")
    private int bill_id;
    
    @Column(name="bill_no")
    private String billno;
    
    @Temporal(value=TemporalType.DATE)
    @Column(name="bill_date")
    private Date billdate;
    
    @Column(name="bill_amt")
    private int amount;
    
    @Column(name="bill_description")
    private String description;
    
    @Column(name="vendor_id")
    private long vendor_id;
    
    public Bills() {
    
    }

    public Bills(String billno, Date billdate, int amount, String description, long vendor_id) {
        super();
        this.billno = billno;
        this.billdate = billdate;
        this.amount = amount;
        this.description = description;
        this.vendor_id = vendor_id;
    }

    public String getBillno() {
        return billno;
    }

    public void setBillno(String billno) {
        this.billno = billno;
    }

    public Date getBilldate() {
        return billdate;
    }

    public void setBilldate(Date billdate) {
        this.billdate = billdate;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public long getBill_id() {
        return bill_id;
    }
    

    public void setBill_id(int bill_id) {
        this.bill_id = bill_id;
    }

    public long getVendor_id() {
        return vendor_id;
    }

    @Override
    public String toString() {
        return "Bills [bill_id=" + bill_id + ", billno=" + billno + ", billdate=" + billdate + ", amount=" + amount
                + ", description=" + description + ", vendor_id=" + vendor_id + "]";
    }
    
    
    

}

Spring Controller (Proving controller only because service and repository is correct)

   @PostMapping("/addbill")
    public Bills addBill(@RequestBody Bills thebill ) {
        System.out.println("Date : "+thebill.getBilldate());
        thebill.setBill_id(0);
        thebillservice.save(thebill);
        return thebill;
    }

On Printing bill object received in Request Body :

Bill Object : Bills [bill_id=0, billno=100, billdate=null, amount=5000, description=Buy Books, vendor_id=0]

After running I am getting following error message :

java.sql.SQLIntegrityConstraintViolationException: Column 'bill_date' cannot be null

Bill Date Column in Bill Entity is of type Date .

Same way i am saving vendor data which is working fine but only thing is there is no Date to save .

Can some one help me why i am getting null values for date attribute.

3
  • Where is the code for Bills? Commented Nov 13, 2020 at 15:38
  • @Smutje I've added Bills Code Commented Nov 13, 2020 at 16:03
  • I had similar problem and well i got solution here Commented Nov 18, 2021 at 9:17

1 Answer 1

1

Your Entity contains a field

billdate

But the document you are sending via Angular contains a field named

date

So how should Spring do the mapping between the 2? Either you change Angular to send the field as

billdate 

Or you add a mapping on the server that reads the field with the name

date 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.