我必须按照以下要求创建休眠实体。
我对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 + "]";
}
}
我的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"
映射) 最佳答案
订单有很多行。 LINE表中的ORDER_ID列是联接列。因此,订单42的所有行的ORDER_ID列均包含42。没错
但是您也要说Hibernate,这个相同的ORDER_ID列构成了OrderLine的ID。因此,OrderLine应该由其ORDER_ID唯一标识。这显然是不可能的。您需要在行表中唯一标识一行的另一个ID列。
关于java - Hibernate中的Order和OrderLine Relationship映射。在哪里放置@OnetoMany和/或@ManytoOne,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29723828/