我有3个表,其中一个具有另外两个表的组合键(另外2个表上的ID)。我看到许多例子,它们都是基于原始数据类型的组合键。在最好的情况下,我会像这样:
@Entity(name = "inventories")
@Table(name = "inventories")
public class Inventories {
@EmbeddedId
private InventoryId inventoryId;
@MapsId("wh_id")
@JoinColumn(name = "warehouse_id", referencedColumnName = "warehouse_id")
@ManyToOne
private Warehouses warehouses;
@MapsId("prd_id")
@JoinColumn(name = "product_id", referencedColumnName = "product_id")
@ManyToOne
private Products products;
@Column(columnDefinition = "Number(8)")
private int quantity;
}
@Embeddable
public class InventoryId implements Serializable {
private Warehouses wh_id;
private Products prd_id;
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
public int hashCode() {
return super.hashCode();
}
}