英文:
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 = "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]
答案1
得分: 1
使用子类中的父类引用来保存,以便在子类中使用父类。目前,由于缺少父类引用,所以 Cart_id
为空。
在 CartItem
中设置 Cart
,以便将 CartItem
与 Cart
一起保存。
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 = "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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论