java - JPA Mapping of Association table where one of the entities has composite key -


i have following model need annotate using jpa:

  • merchant(merchant_id, ...).

  • merchanttype(id1, id2, ...)

  • merchantmerchanttypeassociationtable(merchant_id, id1, id2)

i cannot figure out how map association table. mapping merchant straitghtforward, leave outside of mappings. other mappings follows:

merchanttype:

@entity class merchanttype {      @embeddedid     @attributeoverrides({          @attributeoverride(name = "e1_id", column=@column(name="e1_id")),          @attributeoverride(name = "another_id", column=@column(name="another_id"))     })             merchanttypepk id;      @manytoone     @joincolumn(name = "e1_id", referencedcolumnname = "e1_id", insertable = false, nullable = false)     @mapsid("e1_id")     anotherentity1 e1;       @column(name = "another_id", referencedcolumnname = "another_id", insertable = false, nullable = false)     long anotherid;      //two other local fields irrelevant discussion here      public merchanttype(){         this.id = new merchanttypepk();     }      //getters , setters here. }  //merchanttypepk simple embeddable class here below 2 long fields: //e1_id , another_id 

merchantmerchanttypeassociation:

@entity class merchantmerchanttypeassociation {       @embeddedid      @attributeoverrides({            @attributeoverride(name = "e1_id", column = @column(name = "e1_id")),            @attributeoverride(name = "another_id", column = @column(name = "another_id"))            @attributeoverride(name = "offer_id", column = @column(name = "merchant_id"))      })      private merchantmerchanttypeassociationpk id;      //******** here question      //******** here question      //******** here question      @manytoone      @joincolumns({           @joincolumn(name = "e1_id", referencedcolumnname = "e1_id", insertable = false, updatable = false),           @joincolumn(name = "another_id", referencedcolumnname = "another_id", insertable = false, updatable = false)      })      @mapsid("e1_id")      @mapsid("another_id")      private merchanttype merchanttype;       //similar mapping 1 above, 1 join column        private merchant merchant;       //one more local field irrelevant mapping      //but 1 forcing me map many - - many relationship      //in way.  }  //merchantmerchanttypeassociationpk simple embeddable 

question: how can make mapping kind of entities when annotation '@mapsid' cannot repeated , not accept more 1 value?

you did not include code merchantmerchanttypeassociationpk, i'm guessing looks this:

@embeddable public class merchantmerchanttypeassociationpk {     public merchantpk merchantpk;     public merchanttypepk merchanttypepk; } 

@mapsid used specify attribute within composite key relationship attribute corresponds, not columns. merchantmerchanttypeassociation should this:

@entity class merchantmerchanttypeassociation {      @embeddedid     private merchantmerchanttypeassociationpk id;      @manytoone     @joincolumns({          @joincolumn(name = "e1_id", referencedcolumnname = "e1_id",...),          @joincolumn(name = "e2_id", referencedcolumnname = "e2_id",...)     })     @mapsid("merchanttypepk") // <<< *attribute* in embeddable     private merchanttype merchanttype;      @manytoone     @joincolumn(name = "m_id", referencedcolumnname = "merchant_id",...)     @mapsid("merchantpk") // <<< *attribute* in embeddable     private merchant merchant; } 

derived identities discussed in jpa 2.1 spec, section 2.4.1.


Comments