如何在具有2个实体的休眠状态下使用复合键?

时间:2019-07-28 12:09:42

标签: java hibernate orm

我有3个表,其中一个具有另外两个表的组合键(另外2个表上的ID)。我看到许多例子,它们都是基于原始数据类型的组合键。在最好的情况下,我会像这样:

无法确定com.xxxx.model.entity.Products的类型,在表中:库存,对于列:[org.hibernate.mapping.Column(product_id)]

https://imgur.com/a/s9u0ThU

@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();
    }

}

0 个答案:

没有答案