简体   繁体   English

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

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

I have to make Hibernate Entities for following Requirement. 我必须按照以下要求创建休眠实体。 I am confused in Relationship between Order and OrderLine Tables. 我对Order和OrderLine表之间的关系感到困惑。 Currently I am assuming Order(one) to OrderLine(Many) Relationship but don't know for sure. 目前,我假设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 + "]";
    }
}

My SalesOrder Class 我的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 + "]";
    }
}

I am getting below exception 我在例外之下

Repeated column in mapping for entity: com.prax.dto.OrderLine column: ORDER_ID (should be mapped with insert="false" update="false" ) 实体的映射中重复的列: com.prax.dto.OrderLine列: ORDER_ID (应使用insert="false" update="false"映射)

An order has many lines. 订单有很多行。 The column ORDER_ID in the LINE table is the join column. LINE表中的ORDER_ID列是联接列。 So all the lines of the order 42 contain 42 in their ORDER_ID column. 因此,订单42的所有行的ORDER_ID列均包含42。 That's is right. 没错

But you're also saying Hibernate that this same ORDER_ID column constitutes the ID of OrderLine. 但是,您也要说Hibernate,这个相同的ORDER_ID列构成OrderLine的ID。 So, an OrderLine is supposed to be uniquely identified by its ORDER_ID. 因此,OrderLine应该由其ORDER_ID唯一标识。 That is clearly not possible. 这显然是不可能的。 You need another ID column i the line table that uniquely identifies a line. 您需要在行表中唯一标识一行的另一个ID列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM