我有一个购物车表,其中有一个embededKey cartId(customer,product)和称为“数量”的附加列。创建表时,将为主键(客户,产品)和Fk_cart_product(产品)创建索引,但不会为客户外键创建索引。
仍然为产品和客户创建外键约束素,仅缺少客户类别索引。
购物车
@Entity
public class Cart{
@EmbeddedId
private CartId cartId;
@Column( columnDefinition="INTEGER(10) UNSIGNED")
private Integer quantity;
public Cart(CartId cartId, Integer quantity) {
super();
this.cartId = cartId;
this.quantity = quantity;
}
public CartId getCartId() {
return cartId;
}
public void setCartId(CartId cartId) {
this.cartId = cartId;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
}
// CartId
@Embeddable
public class CartId implements Serializable{
private static final long serialVersionUID = 1L;
@ManyToOne
@JoinColumn(name="product_id",columnDefinition="INTEGER(10)
UNSIGNED",foreignKey=@ForeignKey(name="Fk_cart_product"))
private Product product;
@ManyToOne
@JoinColumn(name="customer_id",columnDefinition="INTEGER(10)
UNSIGNED",foreignKey=@ForeignKey(name="Fk_cart_customer"))
private Customer customer;
public CartId(Product product, Customer customer) {
super();
this.product = product;
this.customer = customer;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}