简体   繁体   中英

Hibernate IdClass referencing a primary composite key

How can I reference a composite primary key in hibernate?

IdClass(ProductItem.ProductItemPK.class) 
public class ProductItem implements Serializable{
    @Id
    @Column(name="ITEM_ID")
    private int itemId;

    @Id
    @JoinColumns({
        @JoinColumn(name="VENDOR_ID", referencedColumnName="VENDOR_ID"),
        @JoinColumn(name="ORDER_ID", referencedColumnName="ORDER_ID")
    })
    @ManyToOne
    private ProductVendor productVendor;

    public static class ProductItemPK implements Serializable{
               //How to reference ids??
    }
}

Product Vendor Class already has a composite key!

@IdClass(ProductVendor.ProductVendorPK.class)
public class ProductVendor implements Serializable{
     @Id
     @JoinColumn(name="VENDOR_ID")
     @ManyToOne
     private Vendor vendor;

     @Id
     @JoinColumn(name="ORDER_ID")
     @ManyToOne
     private Order order;

     public static class ProductVendorPK implements Serializable{

         protected int vendor;
         protected long order;

         //equals and hashCode

}

You map the ProductVendorPK , as if it were a simple field:

public static class ProductItemPK implements Serializable {
    private int itemId;
    private ProductVendorPK productVendor;

    // REST OF CLASS AS PER SPECS...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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