6
Tôi hiện đang sử dụng mã như thế này để thêm mục nhập mới vào bộ trong thực thể của tôi.Chèn vào bộ sưu tập JPA mà không cần tải nó
player = em.find(Player.class, playerId);
player.getAvatarAttributeOwnership().add(new AvatarAttributeOwnership(...));
Nó hoạt động, nhưng mỗi khi tôi muốn thêm một mục, toàn bộ bộ được tải.
- Có cách nào (có thể truy vấn) để thêm mục mà không tải phần còn lại không? Trong SQL, nó sẽ giống như là
INSERT INTO AvatarAttributeOwnership(player, data, ...) VALUES({player}, ...);
Tính duy nhất hiện tại được duy trì theo hợp đồngSet
vàAvatarAttributeOwnership.equals
, nhưng tôi cho rằng sẽ không hoạt động nữa. Làm thế nào tôi có thể thực thi nó?
Tôi đang sử dụng JPA2 + Hibernate. Mã số:
@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;
}
...
}
bạn đã xác nhận công trình đề nghị của tôi? – Bozho
@Bozho Chưa, vì hiện tại tôi cần tập trung vào việc làm một số việc khác thay vì tối ưu hóa điều này, nhưng tôi cho rằng nó hoạt động. –
Tôi cũng giả định, nhưng tôi đã không thử nó, vì vậy bạn có thể bỏ đánh dấu câu trả lời được chấp nhận cho bây giờ :) – Bozho