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

Order and OrderLine Relationship mapping in Hibernate. Where to put @OnetoMany and/or @ManytoOne

我必须为以下要求制作 Hibernate 实体。 我对 Order 和 OrderLine 表之间的关系感到困惑。目前我假设 Order(one) 到 OrderLine(Many) 关系但不确定。

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 + "]";
    }
}

我的销售订单Class

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 + "]";
    }
}

我低于异常

Repeated column in mapping for entity: com.prax.dto.OrderLine column: ORDER_ID (should be mapped with insert="false" update="false")

一个订单有多行。 LINE table 中的列 ORDER_ID 是连接列。因此,订单 42 的所有行都在其 ORDER_ID 列中包含 42。没错。

但是您还对 Hibernate 说同样的 ORDER_ID 列构成了 OrderLine 的 ID。因此,OrderLine 应该由其 ORDER_ID 唯一标识。这显然是不可能的。您需要在行 table 中添加另一个 ID 列来唯一标识一行。