mysql - Spring JPA OneToMany 重复条目 MySQLConstraint 违反错误
问题描述
我有 3 个简单的实体(用户、披萨、订单)。
我想要一个将用户与比萨饼列表链接的 Order 实体,但我有一个“重复条目”错误( MySQLIntegrityConstraintViolationException ):
2018-05-01 21:10:12.145 DEBUG 3493 --- [nio-8080-exec-9] org.hibernate.SQL : select next_val as id_val from hibernate_sequence for update
2018-05-01 21:10:12.146 DEBUG 3493 --- [nio-8080-exec-9] org.hibernate.SQL : update hibernate_sequence set next_val= ? where next_val=?
2018-05-01 21:10:12.153 DEBUG 3493 --- [nio-8080-exec-9] org.hibernate.SQL : insert into comanda (user_id, id) values (?, ?)
2018-05-01 21:10:12.153 DEBUG 3493 --- [nio-8080-exec-9] org.hibernate.SQL : insert into comanda_pizza (order_id, pizza_id) values (?, ?)
2018-05-01 21:10:12.154 WARN 3493 --- [nio-8080-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1062, SQLState: 23000
2018-05-01 21:10:12.154 ERROR 3493 --- [nio-8080-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper : Duplicate entry '3' for key 'UK_ky8uvcbwqkhj6w5t0l7ol1t96'
...
2018-05-01 21:10:12.158 ERROR 3493 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [UK_ky8uvcbwqkhj6w5t0l7ol1t96]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '3' for key 'UK_ky8uvcbwqkhj6w5t0l7ol1t96'
这是用户实体
@Data
@Entity
@Table(name = "profile")
public class User implements Serializable {
private static final long serialVersionUID = 201804302205L;
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE )
private Long id;
@Column(nullable = false)
private String name;
}
这是披萨实体
@Data
@Entity
@Table(name = "pizza")
public class Pizza implements Serializable {
private static final long serialVersionUID = 201804302204L;
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE )
private Long id;
@Column(nullable = false)
private String name;
}
这是订单实体
@Data
@Entity
@Table(name = "comanda")
public class Order implements Serializable {
private static final long serialVersionUID = 201804302206L;
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE )
private Long id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Pizza> pizza;
@ManyToOne
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User customer;
public Order(List<Pizza> pizzas) {
pizza = pizzas;
}
}
这是 OrderService
public Order newOrder(Long userId, List<Long> pizzaIds) {
Order order = new Order(new LinkedList<Pizza>());
User user = userservice.getUserById(userId);
order.setCustomer( user );
for( Long p: pizzaIds ) {
Pizza pizza = pizzaservice.getPizzaById(p);
order.getPizza().add(pizza);
}
return repo.save(order); // <--------- ERROR
}
我该如何解决这个问题???
解决方案
代码看起来不错。
很可能您没有添加@Transactional
您的newOrder
方法或您的OrderService
课程。
推荐阅读
- docusignapi - 禁用 Sender 以编辑 api 视图
- apache-royale - 将AS转换为JS时,是否有减少编译器输出的选项?
- vim - 如何根据状态行中的可用空白向 vim 状态行添加可变数量的字符
- sql - 将两个子查询的结果相除,条件为
- r - 在 ggplot2 中绘制大矩阵的直方图比基本 hist() 慢 20 倍
- hyperledger - 有没有办法在超级账本作曲家中加密数据,比如 sha2 或 sha3?
- javascript - 函数参数可以传递到字符串中吗?
- google-cloud-platform - 如何在 GCP 防火墙上阻止亚洲、俄罗斯和德国 ip
- c - 代码中额外的大括号有什么用?它有什么作用?
- javascript - RxJs 结合 http 请求