开发者

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

Consider the 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 the 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 the association setters synchronize with the redundant fields, here rosterId, roundId, and ordinalNbr?:

    // ???
    public void setGroup(Group group)
    {
        this.group = group;
        this.roundId = group.getRoundId();
        this.ordinalNbr = group.getOrdinalNbr();
    }

Thanks


Yes, they should be kept in synch. Although because they are part of the Id you should never be changing these, so it is really only an issue for new objects.

If you do not keep them in synch, then for a new object they will be null/0, which is probably not good. There is no magic in JPA that will keep these in synch for you.

If you read the object from the database, then they will be in synch of coarse, but you are responsible for maintaining your object's state once in memory, including both duplicate fields, and bi-directional mappings.

If you are using JPA 2.0, why bother having the duplicate Ids at all. You can remove the routersId and the roundId and just add the @Id to the @ManyToOnes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜