我正在使用Spring Boot / Spring Data JPA(休眠)。我有两个实体,WorkOrder
和Inspection
。一个WorkOrder
可以有多个Inspection
,但是一个Inspection
只能绑定到一个WorkOrder
。当WorkOrder
有一个或多个Inspection
时,需要一些额外的信息,因此我也创建了一个WorkOrderInspection
实体。以下是课程。通过这种配置,我得到了一个Hibernate映射异常:
以下字段的断开列映射:com.couplingcorp.qualitycontrol.domain.inspections.Inspection的workOrderInspection.id
我需要更改些什么来解决该错误?还是有一种更好的方法可以完全将其映射?
工作单
@Entity
@Table(name="manufacturing.workorders")
public class WorkOrder implements Serializable, Comparable<WorkOrder> {
private int workOrderID;
private List<WorkOrderInspection> inspections = new ArrayList<>();
...
@Id
@Column(name="pk_workorderid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getWorkOrderID() {
return workOrderID;
}
public void setWorkOrderID(int workOrderID) {
this.workOrderID = workOrderID;
}
...
@OneToMany(mappedBy="workOrder",cascade=CascadeType.ALL,orphanRemoval=true)
public List<WorkOrderInspection> getInspections() {
return inspections;
}
public void setInspections(List<WorkOrderInspection> inspections) {
this.inspections = inspections;
}
}
检查
@Entity
@Table(name="qualitycontrol.inspections")
public class Inspection implements Serializable {
...
private int inspectionID;
private WorkOrderInspection workOrderInspection;
...
@Id
@Column(name="pk_inspectionid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getInspectionID() {
return inspectionID;
}
public void setInspectionID(int inspectionID) {
this.inspectionID = inspectionID;
}
...
@OneToOne(mappedBy="inspection")
public WorkOrderInspection getWorkOrderInspection() {
return workOrderInspection;
}
public void setWorkOrderInspection(WorkOrderInspection workOrderInspection) {
this.workOrderInspection = workOrderInspection;
}
}
WorkOrderInspection
@Entity
@Table(name="manufacturing.workorders_inspections")
@IdClass(WorkOrderInspectionID.class)
public class WorkOrderInspection implements Serializable {
private static final long serialVersionUID = 1L;
private WorkOrder workOrder;
private Inspection inspection;
private int dwgXPosition;
private int dwgYPosition;
public WorkOrderInspection() {
}
@Id
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="fk_workorderid", referencedColumnName="pk_workorderid")
public WorkOrder getWorkOrder() {
return workOrder;
}
public void setWorkOrder(WorkOrder workOrder) {
this.workOrder = workOrder;
}
@Id
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="fk_inspectionid", referencedColumnName="pk_inspectionid")
public Inspection getInspection() {
return inspection;
}
public void setInspection(Inspection inspection) {
this.inspection = inspection;
}
@Column(name="dwgxposition", columnDefinition="decimal(4,0)")
public int getDwgXPosition() {
return dwgXPosition;
}
public void setDwgXPosition(int dwgXPosition) {
this.dwgXPosition = dwgXPosition;
}
@Column(name="dwgyposition", columnDefinition="decimal(4,0)")
public int getDwgYPosition() {
return dwgYPosition;
}
public void setDwgYPosition(int dwgYPosition) {
this.dwgYPosition = dwgYPosition;
}
@Override
public int hashCode() {
int hash = 5;
hash = 97 * hash + Objects.hashCode(this.workOrder);
hash = 97 * hash + Objects.hashCode(this.inspection);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final WorkOrderInspection other = (WorkOrderInspection) obj;
if (!Objects.equals(this.workOrder, other.workOrder)) {
return false;
}
if (!Objects.equals(this.inspection, other.inspection)) {
return false;
}
return true;
}
}
WorkOrderInspectionID
public class WorkOrderInspectionID implements Serializable {
private static final long serialVersionUID = 1L;
private WorkOrder workOrder;
private Inspection inspection;
public WorkOrderInspectionID() {}
public WorkOrderInspectionID(WorkOrder workOrder, Inspection inspection) {
this.workOrder = workOrder;
this.inspection = inspection;
}
public WorkOrder getWorkOrder() {
return workOrder;
}
public void setWorkOrder(WorkOrder workOrder) {
this.workOrder = workOrder;
}
public Inspection getInspection() {
return inspection;
}
public void setInspection(Inspection inspection) {
this.inspection = inspection;
}
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + Objects.hashCode(this.workOrder);
hash = 97 * hash + Objects.hashCode(this.inspection);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final WorkOrderInspectionID other = (WorkOrderInspectionID) obj;
if (!Objects.equals(this.workOrder, other.workOrder)) {
return false;
}
if (!Objects.equals(this.inspection, other.inspection)) {
return false;
}
return true;
}
}