java - Is it considered a best practice to synchronize redundant column properties with associations in JPA 1.0 @IdClass implementations? -


consider following table:

create table participations (   roster_id integer not null,   round_id integer not null,   ordinal_nbr smallint not null ,   was_withdrawn boolean not null,   primary key (roster_id, round_id, ordinal_nbr),   constraint participations_rosters_fk foreign key (roster_id) references rosters (id),   constraint participations_groups_fk foreign key (round_id, ordinal_nbr) references groups (round_id , ordinal_nbr) ) 

here jpa 1.0 @idclass entity class:

@entity @table(name = "participations") @idclass(value = participationid.class) public class participation implements serializable {     @id     @column(name = "roster_id", insertable = false, updatable = false)     private integer rosterid;      @id     @column(name = "round_id", insertable = false, updatable = false)     private integer roundid;      @id     @column(name = "ordinal_nbr", insertable = false, updatable = false)     private integer ordinalnbr;      @column(name = "was_withdrawn")     private boolean waswithdrawn;      @manytoone     @joincolumn(name = "roster_id", referencedcolumnname = "id")     private roster roster = null;      @manytoone     @joincolumns(value = {@joincolumn(name = "round_id", referencedcolumnname = "round_id"), @joincolumn(name = "ordinal_nbr", referencedcolumnname = "ordinal_nbr")})     private group group = null;      public participation()     {     }      public integer getrosterid()     {         return rosterid;     }      public void setrosterid(integer rosterid)     {         this.rosterid = rosterid;     }      public integer getroundid()     {         return roundid;     }      public void setroundid(integer roundid)     {         this.roundid = roundid;     }      public integer getordinalnbr()     {         return ordinalnbr;     }      public void setordinalnbr(integer ordinalnbr)     {         this.ordinalnbr = ordinalnbr;     }      public boolean getwaswithdrawn()     {         return waswithdrawn;     }      public void setwaswithdrawn(boolean waswithdrawn)     {         this.waswithdrawn = waswithdrawn;     }      public roster getroster()     {         return roster;     }      // ???     public void setroster(roster roster)     {         this.roster = roster;     }      public group getgroup()     {         return group;     }      // ???     public void setgroup(group group)     {         this.group = group;     }      ... } 

in general, should association setters synchronize redundant fields, here rosterid, roundid, , ordinalnbr?:

    // ???     public void setgroup(group group)     {         this.group = group;         this.roundid = group.getroundid();         this.ordinalnbr = group.getordinalnbr();     } 

thanks

yes, should kept in synch. although because part of id should never changing these, issue new objects.

if not keep them in synch, new object null/0, not good. there no magic in jpa keep these in synch you.

if read object database, in synch of coarse, responsible maintaining object's state once in memory, including both duplicate fields, , bi-directional mappings.

if using jpa 2.0, why bother having duplicate ids @ all. can remove routersid , roundid , add @id @manytoones.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -