javahibernatespring-data-jpaspring-boot-jpa

JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column


Spring Boot 2.2

I has many CartItem in one Cart. One CartItem has one Product with quantity = n

So here models:

@Entity
public class Cart {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @NotNull
    private String username;
    @NotNull
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    private Date created;
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    private Date updated;
    @OneToMany(mappedBy = "cart", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<CartEntity> productEntities = new HashSet<>();
    @NotNull
    private double totalAmount;
    @NotNull
    private String currency;

    public void addProduct(Product product, int quantity) {
        productEntities.add(new CartEntity(product, quantity, this));
        totalAmount = totalAmount + product.getPrice();
        if (currency == null) {
            currency = product.getCurrency();
        }
    }


@Entity
public class CartEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @NotNull
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    private Date created;
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    private Date updated;
    private int quantity;
    @JoinColumn(name = "order_id")
    @ManyToOne(fetch = FetchType.LAZY)
    private Orders orders;
    @OneToOne(cascade = CascadeType.ALL)
    private Product product;
    @JoinColumn(name = "cart_id")
    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    private Cart cart;

    public CartEntity(Product product, int quantity, Cart cart) {
        this.created = new Date();
        this.product = product;
        this.quantity = quantity;
        this.cart = cart;
    }

    public CartEntity(Product product, int quantity, Orders orders) {
        this.created = new Date();
        this.product = product;
        this.quantity = quantity;
        this.orders = orders;
    }


@Entity
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @NotNull
    private String name;
    private String description;
    @NotNull
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    private Date created;
    @DateTimeFormat(pattern = "dd.MM.yyyy HH:mm:ss")
    private Date updated;
    @NotNull
    private double price;
    @NotNull
    private String currency;
    @ElementCollection
    private Set<String> images;
    @OneToOne(mappedBy = "product", fetch = FetchType.EAGER)
    private CartEntity cartEntity;

    public CartEntity getCartEntity() {
        return cartEntity;
    }

    public void setCartEntity(CartEntity cartEntity) {
        this.cartEntity = cartEntity;
    }

So here my `POST

/cart/product
{
  "user_name": "admin@admin.com",
  "product": {
    "name": "product name",
    "description": "product description",
    "created": "2020-04-10T20:34:15",
    "price": 10.15,
    "currency": "USD",
    "images": [
      "http://www.gravatar.com/avatar/11111?s=200x200&d=identicon",
      "http://www.gravatar.com/avatar/22222?s=200x200&d=identicon"
    ]
  },
  "quantity": 2
}

Here my controller:

 @PostMapping("/cart/product")
    public Response addProduct(@RequestBody Map<String, Object> payloadMap) {
        logger.info("addProduct: payloadMap: " + payloadMap);
        String userName = payloadMap.get("user_name").toString();
        final Product product = new ObjectMapper().convertValue(payloadMap.get("product"), Product.class);
        int quantity = (int) payloadMap.get("quantity");
        Cart findCart = cartRepository.findByUsername(userName);
        if (findCart == null) {
            Cart cart = new Cart();
            cart.setCreated(new Date());
            cart.setUsername(userName);
            cart.addProduct(product, quantity);
            logger.info("addProduct: before_save_new_cart: " + cart);
            cartRepository.save(cart);
            logger.info("addProduct: success_add_product_to_new_cart: " + cart);
            return ResponseService.getSuccessResponse(GsonUtil.gson.toJson(cart.getId()));
        } else {
            //findCart.addProducts(product, quantity);
            logger.info("addProduct: before_save_exist_cart: " + findCart);
            cartRepository.save(findCart);
            logger.info("addProduct: success_add_product_to_exist_cart: " + findCart);
            return ResponseService.getSuccessResponse(GsonUtil.gson.toJson(findCart.getId()));
        }
    }

But in this line:

cartRepository.save(cart);


I get the next error:


   2020-04-11 18:11:18.903  WARN 3319 --- [nio-8092-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23502, SQLState: 23502
2020-04-11 18:11:18.903 ERROR 3319 --- [nio-8092-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : NULL not allowed for column "CART_ID"; SQL statement:
insert into product (created, currency, description, name, price, updated, id) values (?, ?, ?, ?, ?, ?, ?) [23502-200]
2020-04-11 18:11:18.913 ERROR 3319 --- [nio-8092-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/api/v1] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "CART_ID"; SQL statement:
insert into product (created, currency, description, name, price, updated, id) values (?, ?, ?, ?, ?, ?, ?) [23502-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:459) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.get(DbException.java:205) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.get(DbException.java:181) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.table.Column.validateConvertUpdateSequence(Column.java:374) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.table.Table.validateConvertUpdateSequence(Table.java:845) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.dml.Insert.insertRows(Insert.java:187) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.dml.Insert.update(Insert.java:151) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.CommandContainer.update(CommandContainer.java:198) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.Command.executeUpdate(Command.java:251) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:191) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:152) ~[h2-1.4.200.jar:1.4.200]
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-3.4.2.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.2.jar:na]
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3235) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3760) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:107) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
    at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:478) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
    at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:na]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:475) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:348) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:40) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
    at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1352) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:443) ~[hibernate-core-5.4.13.Final.jar:5.4.13.Final]

Solution

  • Use parent reference in child to save using parent.Currently, parent reference is missing thats why Cart_id is null.

    Set Cart in CartItem to save CartItem with Cart

    cartItems.add(new CartItem(product, quantity, tbis));
    

    Update: Use this relation and add cartItem in product and set cartItem in product.

    public class Cart {
    @OneToMany(mappedBy = "cart", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
        private Set<CartItem> cartItems = new HashSet<>();
    }
    
    public class CartItem {
        @ManyToOne(fetch = FetchType.EAGER, optional = false)
        private Cart cart;
        @OneToOne(cascade = CascadeType.ALL)
        private Product product;
    }
    
    public class Product {
        @OneToOne(mappedBy = "product", fetch = FetchType.EAGER)
        private CartItem cartItem;
    }