JdbcSQLIntegrityConstraintViolationException: NULL不允许用于列

huangapple go评论69阅读模式
英文:

JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column

问题

Spring Boot 2.2

在一个购物车(Cart)中可以有多个购物项(CartItem)。每个购物项(CartItem)与一个产品(Product)关联,并且具有数量(quantity)= n。

因此,以下是模型示例:

@Entity
public class Cart {
    // ... 省略其他属性和方法 ...
    @OneToMany(mappedBy = "cart", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<CartEntity> productEntities = new HashSet<>();
    // ... 省略其他属性和方法 ...
    
    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 {
    // ... 省略其他属性和方法 ...
    @OneToOne(cascade = CascadeType.ALL)
    private Product product;
    @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;
    }
    // ... 省略其他属性和方法 ...
}

@Entity
public class Product {
    // ... 省略其他属性和方法 ...
    @OneToOne(mappedBy = "product", fetch = FetchType.EAGER)
    private CartEntity cartEntity;
    // ... 省略其他属性和方法 ...
}

此外,以下是控制器的示例:

@PostMapping("/cart/product")
public Response addProduct(@RequestBody Map<String, Object> payloadMap) {
    // ... 省略部分代码 ...
    Cart findCart = cartRepository.findByUsername(userName);
    if (findCart == null) {
        Cart cart = new Cart();
        // ... 省略部分代码 ...
        cartRepository.save(cart);
        // ... 省略部分代码 ...
    } else {
        // ... 省略部分代码 ...
        cartRepository.save(findCart);
        // ... 省略部分代码 ...
    }
}

然而,在以下代码行:

cartRepository.save(cart);

您遇到了错误:

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]

以上是您遇到的错误信息。

英文:

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 = &quot;dd.MM.yyyy HH:mm:ss&quot;)
private Date created;
@DateTimeFormat(pattern = &quot;dd.MM.yyyy HH:mm:ss&quot;)
private Date updated;
@OneToMany(mappedBy = &quot;cart&quot;, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set&lt;CartEntity&gt; productEntities = new HashSet&lt;&gt;();
@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 = &quot;dd.MM.yyyy HH:mm:ss&quot;)
private Date created;
@DateTimeFormat(pattern = &quot;dd.MM.yyyy HH:mm:ss&quot;)
private Date updated;
private int quantity;
@JoinColumn(name = &quot;order_id&quot;)
@ManyToOne(fetch = FetchType.LAZY)
private Orders orders;
@OneToOne(cascade = CascadeType.ALL)
private Product product;
@JoinColumn(name = &quot;cart_id&quot;)
@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 = &quot;dd.MM.yyyy HH:mm:ss&quot;)
private Date created;
@DateTimeFormat(pattern = &quot;dd.MM.yyyy HH:mm:ss&quot;)
private Date updated;
@NotNull
private double price;
@NotNull
private String currency;
@ElementCollection
private Set&lt;String&gt; images;
@OneToOne(mappedBy = &quot;product&quot;, 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
{
&quot;user_name&quot;: &quot;admin@admin.com&quot;,
&quot;product&quot;: {
&quot;name&quot;: &quot;product name&quot;,
&quot;description&quot;: &quot;product description&quot;,
&quot;created&quot;: &quot;2020-04-10T20:34:15&quot;,
&quot;price&quot;: 10.15,
&quot;currency&quot;: &quot;USD&quot;,
&quot;images&quot;: [
&quot;http://www.gravatar.com/avatar/11111?s=200x200&amp;d=identicon&quot;,
&quot;http://www.gravatar.com/avatar/22222?s=200x200&amp;d=identicon&quot;
]
},
&quot;quantity&quot;: 2
}

Here my controller:

 @PostMapping(&quot;/cart/product&quot;)
public Response addProduct(@RequestBody Map&lt;String, Object&gt; payloadMap) {
logger.info(&quot;addProduct: payloadMap: &quot; + payloadMap);
String userName = payloadMap.get(&quot;user_name&quot;).toString();
final Product product = new ObjectMapper().convertValue(payloadMap.get(&quot;product&quot;), Product.class);
int quantity = (int) payloadMap.get(&quot;quantity&quot;);
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(&quot;addProduct: before_save_new_cart: &quot; + cart);
cartRepository.save(cart);
logger.info(&quot;addProduct: success_add_product_to_new_cart: &quot; + cart);
return ResponseService.getSuccessResponse(GsonUtil.gson.toJson(cart.getId()));
} else {
//findCart.addProducts(product, quantity);
logger.info(&quot;addProduct: before_save_exist_cart: &quot; + findCart);
cartRepository.save(findCart);
logger.info(&quot;addProduct: success_add_product_to_exist_cart: &quot; + 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 &quot;CART_ID&quot;; 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 &quot;CART_ID&quot;; 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]

答案1

得分: 1

使用子类中的父类引用来保存,以便在子类中使用父类。目前,由于缺少父类引用,所以 Cart_id 为空。

CartItem 中设置 Cart,以便将 CartItemCart 一起保存。

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

更新:使用这种关系,在产品中添加 cartItem,并在产品中设置 cartItem

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;
}
英文:

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 = &quot;cart&quot;, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set&lt;CartItem&gt; cartItems = new HashSet&lt;&gt;();
}
public class CartItem {
@ManyToOne(fetch = FetchType.EAGER, optional = false)
private Cart cart;
@OneToOne(cascade = CascadeType.ALL)
private Product product;
}
public class Product {
@OneToOne(mappedBy = &quot;product&quot;, fetch = FetchType.EAGER)
private CartItem cartItem;
}

huangapple
  • 本文由 发表于 2020年4月11日 03:40:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61147531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定