6
我目前正在使用這樣的代碼向我的實體中的集合添加一個新條目。插入JPA集合而不加載它
player = em.find(Player.class, playerId);
player.getAvatarAttributeOwnership().add(new AvatarAttributeOwnership(...));
它的工作原理,但每次我想添加一個項目,整個集合加載。
- 有沒有一種方法(與查詢也許)添加項目而不加載其餘的?在SQL中,它將類似
INSERT INTO AvatarAttributeOwnership(player, data, ...) VALUES({player}, ...);
目前唯一性由Set
和AvatarAttributeOwnership.equals
的合同維護,但我認爲這將不再有效。我怎樣才能執行它呢?
我正在使用JPA2 + Hibernate。代碼:
@Entity
public class Player implements Serializable {
@Id
@GeneratedValue
private long id;
@ElementCollection(fetch=FetchType.LAZY)
// EDIT: answer to #2
@CollectionTable([email protected](columnNames={"Player_id","gender","type","attrId"}))
Set<AvatarAttributeOwnership> ownedAvatarAttributes;
...
}
@Embeddable
public class AvatarAttributeOwnership implements Serializable {
@Column(nullable=false,length=6)
@Enumerated(EnumType.STRING)
private Gender gender;
@Column(nullable=false,length=20)
private String type;
@Column(nullable=false,length=50)
private String attrId;
@Column(nullable=false)
private Date since;
@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 (!type.equals(other.type)) return false;
return true;
}
...
}
你確認我的建議的作品? – Bozho
@Bozho還沒有,因爲我目前需要專注於使其他一些工作而不是優化,但我認爲它的工作原理。 –
我也假設,但我沒有嘗試過,所以你現在可以取消接受的答案:) – Bozho