Ich habe diese Entitäten in JPA + Hibernate, Player
y AvatarAttributeOwnership
. Ersteres hat eine Set
des letzteren, der definiert, welche Avatarattribute er besitzt.
@Entity
public class Player implements Serializable {
@Id
@GeneratedValue
private long id;
@OneToMany(fetch=FetchType.LAZY,mappedBy="owner",cascade=CascadeType.ALL)
private Set<AvatarAttributeOwnership> ownedAvatarAttributes;
...
}
@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"owner","gender","type","attrId"}))
public class AvatarAttributeOwnership implements Serializable {
@Id
@GeneratedValue
@SuppressWarnings("unused")
private long id;
@ManyToOne
@JoinColumn(name="owner")
private Player owner;
// these three columns define the attribute (i.e. are attribute's key)
@Column
private String attrId;
@Column
private String gender;
@Column
private String type;
@Column
private Date ownedSince;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + attrId.hashCode();
result = prime * result + gender.hashCode();
result = prime * result + owner.hashCode();
result = prime * result + type.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
AvatarAttributeOwnership other = (AvatarAttributeOwnership) obj;
if (!attrId.equals(other.attrId)) return false;
if (gender != other.gender) return false;
if (!owner.equals(other.owner)) return false;
if (!type.equals(other.type)) return false;
return true;
}
}
AvatarAttributeOwnerships werden auf diese Weise erstellt:
player = em.find(Player.class, playerId);
player.getAvatarAttributeOwnership().add(new AvatarAttributeOwnership(player, attrId, gender, type, new Date()));
em.persist(player);
Kann ich den Verweis auf den Eigentümer in AvatarAttributeOwnership
und dabei die dort definierte eindeutige Einschränkung beibehalten?