简体   繁体   中英

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.

How do I get list of Items by using order 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.

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. Maybe there are also other issues, but this is an obvious one that I can spot without seeing the error.

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