[英]New entity in a set is not persisted
JPA + Hibernate JPA + Hibernate
I'm using this code to create a new AvatarAttributeOwnership
( AAO
) and assign it to a Player.我正在使用此代码创建一个新的
AvatarAttributeOwnership
( AAO
) 并将其分配给 Player。 However, the new AAO
is not persisted to the database.但是,新的
AAO
不会持久化到数据库中。
// get player
player = em.find(Player.class, playerId);
....
// give attribute
player.getAvatarAttributeOwnership().add(new AvatarAttributeOwnership(player, atr.key()));
// save
em.persist(player);
Entities:实体:
@Entity
public class Player implements Serializable {
@Id
@GeneratedValue
private long id;
@OneToMany(fetch=FetchType.LAZY,mappedBy="owner")
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;
...
@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;
}
}
Not sure where to place it but I think you need a cascade="all"
attribute probably in players' onetomany (not sure I'm still using hbm files).不确定在哪里放置它,但我认为您可能需要一个
cascade="all"
属性,可能在玩家的 onetomany 中(不确定我是否仍在使用 hbm 文件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.