JPA many to many with extra column

前端 未结 3 1450
小蘑菇
小蘑菇 2020-12-20 12:45

I have a following problem that I need to solve. The core issues is that I want to add additional column into JoinTable for ManyToMany relation in JPA. In my case I have fol

相关标签:
3条回答
  • 2020-12-20 12:58

    One technique that is useful when creating the many-to-many mapping class entity is to attribute the id's in the class along with @ManyToOne designation which makes this class act as the composite key class:

    @Entity
    @Table(name = "market_vendor")
    public class MarketVendor implements Serializable 
    {
      @Id
      @ManyToOne
      @JoinColumn(name = "market_id")
      private Market market;
    
      @Id
      @ManyToOne
      @JoinColumn(name = "vendor_id")
      private Vendor vendor;
    
      @Basic
      @Column(name="active")
      private boolean active;
    
      public MarketVendor(Market market, Vendor vendor, boolean active)
      {
        this.market = market;
        this.vendor = vendor;
        this.active = active;
      }
    }
    

    This allows you to have the composite primary key defined within the same class without having to have a separate primary key class. You also need to make the class serializable.

    0 讨论(0)
  • 2020-12-20 13:07

    Use list instead of set, together with the @OrderColumn annotation and JPA will automatically take care of the order:

    @MappedSuperclass
    public class BaseEntity{
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        public Long getId(){
            return id;
        }
    
        public void setId(final Long id){
            this.id = id;
        }
    
    }
    
    @Entity
    public class Topic extends BaseEntity{
    
        @ManyToMany(mappedBy = "topics")
        @OrderColumn
        private List<Document> documents = new ArrayList<Document>();
    
        public List<Document> getDocuments(){
            return documents;
        }
    
        public void setDocuments(final List<Document> documents){
            this.documents = documents;
        }
    
    }
    
    @Entity
    public class Document extends BaseEntity{
    
        @ManyToMany
        @OrderColumn
        private List<Topic> topics = new ArrayList<Topic>();
    
        public List<Topic> getTopics(){
            return topics;
        }
    
        public void setTopics(final List<Topic> topics){
            this.topics = topics;
        }
    
    }
    

    Generated DDL (using hibernate and HSQL):

    create table Document (
        id bigint generated by default as identity (start with 1),
        primary key (id)
    );
    
    create table Document_Topic (
        documents_id bigint not null,
        topics_id bigint not null,
        topics_ORDER integer not null,
        documents_ORDER integer not null,
        primary key (documents_id, topics_ORDER)
    );
    
    create table Topic (
        id bigint generated by default as identity (start with 1),
        primary key (id)
    );
    
    alter table Document_Topic 
        add constraint FK343B5D0B481100B2 
        foreign key (documents_id) 
        references Document;
    
    alter table Document_Topic 
        add constraint FK343B5D0B558627D0 
        foreign key (topics_id) 
        references Topic;
    
    0 讨论(0)
  • 2020-12-20 13:08

    I would try to avoid using a List unless you allow duplicates.

    There is a @OrderColumn annotation that automatically does this. Have you tried it?

    @Entity
    public class Topic {
     @Id
     private Long id;
     @Basic
     private String name;
    
     @OneToMany
     @OrderColumn
     private Set<TopicToRemoteDocument> association;
    }
    
    0 讨论(0)
提交回复
热议问题