我有一个问题,即使我明确地设置它们,Hibernate重置实体的ID。休眠设置ID为空
这里是我的两个实体:
@Entity
@Table(name="game")
public class Game implements Serializable {
private String token;
private Set<DealersCard> dealersCards;
@Id
@Size(min=36, max=36)
@Column(name="token")
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
@OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL})
public Set<DealersCard> getDealersCards() {
return dealersCards;
}
public void setDealersCards(Set<DealersCard> dealersCards) {
this.dealersCards = dealersCards;
}
}
和
@Entity
@Table(name="dealers_card")
public class DealersCard implements Serializable {
private String token;
private int id;
public void setVisible(boolean visible) {
this.visible = visible;
}
@ManyToOne
@MapsId("token")
@JoinColumn(name="token", referencedColumnName="token")
public Game getGame() {
return game;
}
public void setGame(Game game) {
this.game = game;
}
@Id
@Size(min = 36, max = 36)
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
当我创建游戏的实例,通过EntityManager的分配两个DealersCard ::持久化对象在数据库中创建正确我可以稍后检索它们,但是如果我检索Game的一个实例,请添加DealersCard的另一个实例,并尝试使用EntityManager :: merge,Hibernate更新数据库,出于某种原因将token字段设置为null,并将id设置为0,我得到
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: ERROR: null value in column "token" violates not-null constraint Detail: Failing row contains (null, 0).
我试过MySQL和PostgreSQL,但我仍然遇到同样的错误。我正在运行Glassfish 3.1.2.2,Hibernate Entity Manager 4.1.2,Java EE 6.
任何帮助表示赞赏。
谢谢!
你是完全正确的 - 问题是映射。谢谢! – Martynas 2013-02-19 00:04:39