java - jpa hibernate composite foreign key mapping -


i having trouble setting jpa mappings entities. have parent entity defined following.

@entity @table(name="eif_instance_hdr") public class instanceentity implements serializable{  private static final long serialversionuid = 1l;  @id @generatedvalue(generator="eif_inst_gen") @sequencegenerator(name="eif_inst_gen",sequencename="eif_instance_seq") @column(name = "eaih_id") private long eaihid; @column(name = "ead_id") private long eadid;  @onetomany(targetentity=instancenotifyentity.class, mappedby="instance",fetch=fetchtype.eager, cascade = cascadetype.all) private list<instancenotifyentity> userdetails = new arraylist<instancenotifyentity>(); } 

i have child entity w/ composite key, , foreign key primary key of table follows:

@entity @table(name="eif_inst_notified") public class instancenotifyentity implements serializable{  private static final long serialversionuid = 1l;  @id @manytoone @joincolumn(name="eaih_id", referencedcolumnname="eaih_id") private instanceentity instance;  @id @column(name="user_id") private long userid; @column(name="comment_txt") private string commenttext; } 

i know child entity incorrect, unsure how set have composite pk. know need setup pk class, not sure how when 1 field foreign key parent class. , once setup how parent reference child entity?

any appreciated.

this governed jpa 2 spec section 2.4.1, "primary keys corresponding derived identities". section contains 2 examples directly applicable problem.

as described in spec, there 2 ways represent child entity's key in case:

  • @idclass
  • @embeddedid

here's rough sketch of embeddedid way. chose embeddedid arbitrarily, choice between idclass , embeddedid significant. might choose differently.

// child entity's composite primary key @embeddable public class instancenotifyentityid implements serializable {     long eaihid;     long userid; }  // child entity @entity @table(name="eif_inst_notified") public class instancenotifyentity implements serializable {     @attributeoverrides({       @attributeoverride(name="userid", column = @column(name="user_id"))       @attributeoverride(name="eaihid", column = @column(name="eaih_id"))     })     @embeddedid     instancenotifyentityid id;      @mapsid("eaihid")     @manytoone     instanceentity instance;      // ...  } 

the parent entity needs 1 change: userdetails attribute mappedby should "id.eaihid". think that's it, haven't used entities before. might have missed something... please post if see errors.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -