Mapping Extra Attribute in a Join Table JPA 2

后端 未结 1 389
天命终不由人
天命终不由人 2021-02-06 08:10

I am trying to model this relationship following this link http://www.javaworld.com/javaworld/jw-01-2008/images/datamodel.gif

Its the usual Many to Many relationship bet

相关标签:
1条回答
  • 2021-02-06 08:56

    There is no concept of having additional persistent attribute in relation in JPA (2.0). That's why relation with property is actually intermediate entity.

    From both Order and Product entities, you need one-to-many relationship to new entity. Because of bidirectional relationship, new entity will have many-to-one relationships to Order and Product.

    You need to go for something like this (shows only relationships, id's and other mappings stripped away):

    @Entity
    @Table(name="order_item")
    public class OrderItem {
        @ManyToOne
        private Order order;
        @ManyToOne
        private Product product;
    }
    
    @Entity
    public class Order {
        @OneToMany (mappedBy = "order")
        private Set<OrderItem> orderItems;
    }
    
    @Entity
    public class Product {
        @OneToMany(mappedBy = "product")
        private Set<OrderItem> orderItems;
    }
    
    0 讨论(0)
提交回复
热议问题