Java 两个实体正在生成奇怪的
CartItem实体Java 两个实体正在生成奇怪的,java,spring,hibernate,Java,Spring,Hibernate,CartItem实体 @Entity @Table(name="cart_item") public class CartItem { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer cartItemId; private int quantity; private double price;
@Entity
@Table(name="cart_item")
public class CartItem {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer cartItemId;
private int quantity;
private double price;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "product_id")
private Product product;
@ManyToOne
@JoinColumn(name = "cart_id")
private Cart cart;
购物车实体
@Entity
@Table(name="cart")
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer cart_id;
@OneToOne
@JoinColumn(name = "user_id")
@JsonIgnore
private User user;
@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL,fetch=FetchType.EAGER)
private List<CartItem> cartItem;
private double totalPrice;
我不知道怎么回事,但我给每个用户分配了1个购物车,每次用户在购物车中放置东西,或者创建购物车时,它都会向两个实体id中添加+1(如果有2个购物车,并且用户将项目添加到购物车ItemCartId将是3)
这是因为您使用的策略是使用相同的序列在多个表中生成ID。将其替换为以下内容可以解决您的问题:
@GeneratedValue(strategy = GenerationType.IDENTITY)
旁注:
double
不是货币值的合适类型。您应该改用BigDecimal
。
@GeneratedValue(strategy = GenerationType.IDENTITY)