JPA @OneToMany与复合PK - PK / FK重叠

时间:2014-02-20 12:39:20

标签: java jpa overlapping composite-primary-key

我需要使用JPA的现有数据库表。这些表使用复合主键。外键属性与表的主键重叠。

简化示例,每个'Order'都有许多'OrderItems'

Table Order                   Table OrderItem
-------------------           ----------------------
businessDomain (PK)           businessDomain (PK/FK)
orderId        (PK)           oItemId        (PK)
field1                        orderId        (FK)
field2 ...                    description ...

我创建了以下实体和ID类:

// ============== Order Entity ====================== 
@Entity
@IdClass(OrderId.class)
public class Order implements Serializable {

    @Id
    private long busindessDomain;

    @Id
    private long orderId;

    private String field1;
    private String field2;

    //Getters/Setters
}

// ============== OrderItem Entity ==================
@Entity
@IdClass(OrderItemId.class)
public class OrderItem implements Serializable {

    @Id
    private long busindessDomain;

    @Id
    private long oItemId;

    private String description;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="orderId", referencedColumnName="orderId", insertable=false, updatable=false)
    private Order order;
    //Getters/Setters
}

// ============== OrderId class =====================
public class OrderId implements Serializable {

    private long busindessDomain;
    private long orderId;

    //Getters/Setters
}

// ============== OrderItemId class =================
public class OrderId implements Serializable {

    private long busindessDomain;
    private long oItemId;

    //Getters/Setters
}

但那不起作用。 那么我需要做些什么来从orderItem获得关于重叠键和复合键的正确映射?

感谢您的帮助。 丹尼尔


编辑:

好的,这是另一种似乎有用的尝试。 需要进一步测试。

// ============== Order Entity ====================== 
@Entity
@IdClass(OrderId.class)
public class Order implements Serializable {

    @Id
    private long busindessDomain;

    @Id
    private long orderId;

    private String field1;
    private String field2;

    @OneToMany(targetEntity=OrderItem.class, mappedBy="order")
    private List<OrderItem> orderItems;

    //Getters/Setters
}

// ============== OrderItem Entity ==================
@Entity
@IdClass(OrderItemId.class)
public class OrderItem implements Serializable {

    @Id
    private long busindessDomain;

    @Id
    private long oItemId;

    private String description;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumns({
        @JoinColumn(name="businessDomain", referencedColumnName="businessDomain"),
        @JoinColumn(name="orderId", referencedColumnName="orderId")
    })
    private Order order;

    //Getters/Setters
}

1 个答案:

答案 0 :(得分:0)

您的外键只是busindessDomain而不是OrderId。你能试试吗?

@Entity    
    public class Order {



    @EmbeddedId
    private OrderPk orderId;

    //getter/setter...

    @Embeddable
    private static class OrderPk {
       private long busindessDomain;
       private long oItemId;
    }       
    }

OrderItem的:

@Entity    
    public class OrderItem{



    @EmbeddedId
    private OrderItemPk orderTemId;

    @ManyToOne
    @JoinColumns(value = { @JoinColumn(referencedColumnName = "businessDomain", name="businessDomain")})
    private Order order;


    getter/setter...

    @Embeddable
    private static class OrderItemPk{
       private long busindessDomain;
       private long oItemId;
    }       
    }