hibernate映射主键和复合键之间的一对多关系

时间:2014-07-07 11:47:06

标签: spring spring-mvc hibernate-mapping hibernate-onetomany

我正在努力解决映射的hibernate映射问题。订单表的主键和Product Cart的复合键之间的一对多关系带有一些额外的列

public class OrderDetails implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@Column(name="ORDERID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer orderId;

@Column(name="ORDER_DATE")
private Date orderDate= new Date();
//other fields and getter setter
.....
.....

产品购物车表具有复合钥匙CART ID和PRODUCT ID

@Entity
@Table(name="PRODUCT_CART")
@AssociationOverrides({
    @AssociationOverride(name="pk.shopCart", joinColumns=@JoinColumn(name="CARTID")),
    @AssociationOverride(name="pk.product", joinColumns=@JoinColumn(name="PRODUCTID"))
})
public class ProductCart implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@EmbeddedId
private ProductCartId pk = new ProductCartId();

@Column(name="QUANTITY")
private Integer selectedQuantity=1;

@Column(name="TOTAL")
private double total=0.0;
//other fields and getter setter
.....
.....

我试过以下但没有工作

@Entity
@Table(name="PRODUCTCART_ORDERDETAILS")
@AssociationOverrides({
@AssociationOverride(name="pcoPK.orderDetails",joinColumns=@JoinColumn(name="ORDERID"))    ,
@AssociationOverride(name="pcoPK.pk", joinColumns=       
    {@JoinColumn(name="pk.shopCart",referencedColumnName="CARTID"),
    @JoinColumn(name="pk.product",referencedColumnName="PRODUCTID") }) })

public class ProductCartOrder implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -2348674131019001487L;

@EmbeddedId
private ProductCartOrderId pcoPK = new ProductCartOrderId();

@Column(name="QUANTITY")
private Integer quantity;

@Column(name="PRICE")
private double price;

@Transient
public OrderDetails getOrderDetails(){
    return getPcoPK().getOrderDetails();
}
public void setOrderDetails(OrderDetails orderDetails){
    getPcoPK().setOrderDetails(orderDetails);
}

@Transient
public ProductCartId getProductCartId(){
    return getPcoPK().getPk();
}
public void setProductCartId(ProductCartId pk){
getPcoPK().setPk(pk);
}

有人可以帮我实现吗?以下是错误消息

引起:org.hibernate.AnnotationException:非法尝试使用mappedBy关联定义@JoinColumn:pcoPK.pk     在org.hibernate.cfg.Ejb3JoinColumn.buildJoinColumn(Ejb3JoinColumn.java:152)     在org.hibernate.cfg.Ejb3JoinColumn.buildJoinColumns(Ejb3JoinColumn.java:127)     在org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1212)     在org.hibernate.cfg.AnnotationBinder.fillComponent(AnnotationBinder.java:1841)     在org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1878)

1 个答案:

答案 0 :(得分:0)

经过大量研究后,我无法找到解决方案。 我创建了OrderDetails和Product之间的多对多关系,并为产品购物车中的每个元素手动创建了一些额外的列ID,价格,数量和插值。

public class Product implements Serializable {
@OneToMany(mappedBy="product")
private Set<ProductOrder> productOrder;
...//other fields and getter setter
}

public class OrderDetails implements Serializable {
@OneToMany(mappedBy="orderDetails")
private Set<ProductOrder> productOrder;
...//other fields and getter setter
}

public class ProductOrder {

@Id
@Column(name="PRODUCT_ORDER_ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int prductOrderId;

@ManyToOne
private OrderDetails orderDetails;

@ManyToOne
private Product product;
...//other fields and getter setter
}

在我想要保存ProductCart产品的控制器类中,我按照

进行了操作
List<ProductCart> productList = new ArrayList<ProductCart>();
    productList=productCartService.getCartProducts(shopCart);

    ProductOrder orderedProducts = new ProductOrder();

    for (ProductCart productCarts : productList) {
        orderedProducts.setOrderDetails(orderDetails);
        orderedProducts.setProduct(productCarts.getPk().getProduct());
        orderedProducts.setPrice(productCarts.getPk().getProduct().getPrice());
        orderedProducts.setQuantity(productCarts.getSelectedQuantity());
        productOrderService.addOrderProducts(orderedProducts);
        }