Symmetric composite key in jpa/hibernate?

First of all, I would not use a composite ID. Use an autogenerated surrogate key, and store both items as regular properties.

Up vote 0 down vote favorite share g+ share fb share tw.

I'm trying to make sure that a model is not persisted twice in the database and its id is symmetrical. Under symmetrical composite id I mean the following: @Entity public class Item { @Id @GeneratedValue(strategy = GenerationType. IDENTITY) @Column(name = "item_id", unique = true, nullable = false) public Long id; // other properties ... } @Entity public class Pair { @EmbeddedId public PairId id; // other properties... @Embeddable public static class PairId implements Serializable { @ManyToOne(cascade={CascadeType.

MERGE, CascadeType. REFRESH, CascadeType. DETACH}) @JoinColumn(name="source_item_id") public Item source; @ManyToOne(cascade={CascadeType.

MERGE, CascadeType. REFRESH, CascadeType. DETACH}) @JoinColumn(name="target_item_id") public Item target; @Override public boolean equals(Object o) { if(this == o){ return true; } if (o instanceof PairId == false){ return false; } PairId other = (PairId) o; return (this.source.

Equals(other. Source) && this.target. Equals(other.

Target)) || (this.source. Equals(other. Target) && this.target.

Equals(other. Source)); } @Override public int hashCode() { //probably not the best approach return source.hashCode() + target.hashCode(); } } } Example: Item i1 = new Item(); Item i2 = new Item(); //persist items into the database ... PairId pId1 = new PairId(i1, i2); PairId pId2 = new PairId(i2, i1); Pair p1 = new Pair(pId1); //persist p1 into the database Pair p2 = new Pair(pId2); //calling persist should not add new entry to the database, since p2 is symmetrical to p1 and already exists in the database Pair p3 = findById(pId2); //p3 should now contain p1 also Do you have any idea how I could implement such a behaviour? Thanks in advance!

Edit: Added comments on both classes in order to show that those classes could have(and they have) other properties except of the listed ids above. But for the sake of simplicity I just left their ids as alone standing property. Java hibernate jpa duplicates composite-key link|improve this question edited Nov 26 '11 at 14:31 asked Nov 25 '11 at 16:54d33t234.

First of all, I would not use a composite ID. Use an autogenerated surrogate key, and store both items as regular properties. Then, when storing the items in a pair, I would just make sure to always store them in the same order.

For example, the source ID should always be smaler than the target ID. This can be ensured using encapsulation: public void setItems(Item i1, Item i2) { if (i1.getId(). CompareTo(i2.getId()) Source = i1; this.

Target = i2; } else { this. Source = i2; this. Target = i1; } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions