简体   繁体   English

JPA @Onetomany和@manytoone通过复合主键的一部分获取

[英]JPA @Onetomany and @manytoone fetch by part of composite primary key

I have two entities of Order and Items. 我有订单和项目的两个实体。 In Order entity the id is composite primary key of OrderId, UserId but in Items the foreign key is only Order Id. 在Order实体中,id是OrderId,UserId的复合主键,而在Items中,外键仅是Order ID。

How do I get list of Items by using order Id 如何使用订单ID获取商品列表

public class Order {
   @EmbeddedId
   private CompositePrimaryKey pk;

   @OneToMany
   @JoinColumn(name="ORDER_ID")
   private List<Items> itemsLst;
}

public Class Items {
   @Id
   @Column(name="ORDER_ID")
   private Integer orderId;
}

The above code is not working. 上面的代码不起作用。 I know that table design is not good, but it is hard to change the design of tables now, that change will affect lot of places. 我知道表格设计不好,但是现在很难更改表格的设计,因为更改会影响很多地方。 So keeping the current design can I do anything on this. 因此,保持当前设计对这一点我可以做任何事情。

I tried using mappedBy and as mentioned above in either case I am getting error. 我尝试使用mapledBy,并且如上所述,在两种情况下我都遇到错误。

Any help is highly appreciated. 非常感谢您的帮助。

If you have a OneToMany between Order and Item, then ORDER_ID cannot be a primary key of the Item because it won't be unique. 如果Order和Item之间有一个OneToMany,则ORDER_ID不能是Item的主键,因为它不是唯一的。 Maybe there are also other issues, but this is an obvious one that I can spot without seeing the error. 也许还有其他问题,但这是一个显而易见的问题,我可以看到而不会看到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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