java.sql.SQLException: Field 'cid' doesn't have a default valueI have added

Viewed 18

I have made sure that there is this strategy added here in the spring boot, but for some reasons it doesn't have a default value assigned it and I get the following exception: java.sql.SQLException: Field 'cid' doesn't have a default value Why is it the case? I will really appreciate your help.

Below is the code:

package com.fsse2207.project_backend.data.cart_item.entity;

import com.fsse2207.project_backend.data.cart_item.CreateCartItemData;
import com.fsse2207.project_backend.data.product.entity.ProductEntity;
import com.fsse2207.project_backend.data.user.entity.UserEntity;
import javax.persistence.*;

@Entity
@Table(name="Cart_Item")
public class CartItemEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="cid",nullable = false)
    Integer cid;
    @OneToOne
    @JoinColumn(name="pid",nullable = false)
    ProductEntity product;
    @ManyToOne
    @JoinColumn(name="uid",nullable = false)
    UserEntity user;
    @Column(name="quantity",nullable = false)
    Integer quantity;

    public CartItemEntity(){

    }
    public  CartItemEntity(CreateCartItemData createCartItemData,ProductEntity product,UserEntity userEntity){
        this.cid=createCartItemData.getCid();
        this.product=product;
        this.user=userEntity;
        this.quantity=createCartItemData.getQuantity();
    }

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }


    public ProductEntity getProduct() {
        return product;
    }

    public void setProduct(ProductEntity product) {
        this.product = product;
    }

    public UserEntity getUser() {
        return user;
    }

    public void setUser(UserEntity user) {
        this.user = user;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

}

Below is the database:

1 Answers

Your cid primary key needs to have a default identity value as the DB is responsible for creating the PK not the orm

CREATE TABLE cart_item (
     cid INT NOT NULL AUTO_INCREMENT,
     ::
     PRIMARY KEY (cid)
);
Related