Hibernate中的Order和OrderLine关系映射。在哪里放@OnetoMany和/或@ManytoOne

时间:2015-04-18 22:54:18

标签: java hibernate java-ee jpa-2.0

我必须为后续要求制作Hibernate实体。 我对Order和OrderLine表之间的关系感到困惑。目前我假设订单(一)到OrderLine(很多)关系,但不确定。

package com.prax.dto;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity(name="ORDERLINE")
public class OrderLine {

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

    @Column(name="PRODUCT_ID")
    private String product;

    @Column(name="UNITPRICE")
    private double unitPrice;

    @Column(name="TOTALPRICE")
    private double totalPrice;

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

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="ORDER_ID", nullable=false)
    private SalesOrder salesOrderMap;

    public OrderLine(){}

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getProduct() {
        return product;
    }

    public void setProduct(String product) {
        this.product = product;
    }

    public double getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public SalesOrder getSalesOrderMap() {
        return salesOrderMap;
    }

    public void setSalesOrderMap(SalesOrder salesOrderMap) {
        this.salesOrderMap = salesOrderMap;
    }

    @Override
    public String toString() {
        return "OrderLine [orderId=" + orderId + ", product=" + product
                + ", unitPrice=" + unitPrice + ", totalPrice=" + totalPrice
                + ", quantity=" + quantity + ", salesOrderMap=" + salesOrderMap
                + "]";
    }
}
package com.prax.dto;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="SALESORDER")
public class SalesOrder {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ORDER_ID")
    private String orderNumber;

    @Column(name="CUSTOMER_ID")
    private String customer;

    @Column(name="TOTALPRICE")
    private double totalPrice;

    @OneToMany(mappedBy="salesOrderMap")
    private List<OrderLine> orderLines;

    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public List<OrderLine> getOrderLines() {
        return orderLines;
    }

    public void setOrderLines(List<OrderLine> orderLines) {
        this.orderLines = orderLines;
    }

    @Override
    public String toString() {
        return "SalesOrder [orderNumber=" + orderNumber + ", customer="
                + customer + ", totalPrice=" + totalPrice + ", orderLines="
                + orderLines + "]";
    }
}

我的SalesOrder类

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="SALESORDER")
public class SalesOrder {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ORDER_ID")
    private String orderNumber;

    @Column(name="CUSTOMER_ID")
    private String customer;

    @Column(name="TOTALPRICE")
    private double totalPrice;

    @OneToMany(mappedBy="salesOrderMap")
    private List<OrderLine> orderLines;

    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public List<OrderLine> getOrderLines() {
        return orderLines;
    }

    public void setOrderLines(List<OrderLine> orderLines) {
        this.orderLines = orderLines;
    }

    @Override
    public String toString() {
        return "SalesOrder [orderNumber=" + orderNumber + ", customer="
                + customer + ", totalPrice=" + totalPrice + ", orderLines="
                + orderLines + "]";
    }
}

我正处于异常

之下
  

实体映射中的重复列:com.prax.dto.OrderLine列:ORDER_ID(应与insert="false" update="false"映射

1 个答案:

答案 0 :(得分:1)

订单有很多行。 LINE表中的ORDER_ID列是连接列。因此,订单42的所有行在其ORDER_ID列中包含42。那是对的。

但是你也在说Hibernate这个相同的ORDER_ID列构成了OrderLine的ID。因此,OrderLine应该由其ORDER_ID唯一标识。这显然是不可能的。您需要另一个ID列,即行表,用于唯一标识一行。