Java Hibernate更新:为什么Hibernate先删除对象,然后再插入对象
我使用Hibernate作为我的项目的orm解决方案,我不知道发生了什么。 我有一个实体用户:Java Hibernate更新:为什么Hibernate先删除对象,然后再插入对象,java,hibernate,Java,Hibernate,我使用Hibernate作为我的项目的orm解决方案,我不知道发生了什么。 我有一个实体用户: @Entity @Table(name="users") public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id; ... @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinTable(name =
@Entity
@Table(name="users")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
...
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "users_projects", joinColumns = {
@JoinColumn(name = "userid")
},
inverseJoinColumns = {
@JoinColumn(name = "projectid")
})
private List<Project> projects;
... +getters and setters
}
只是一个假设:
您的实体在更新之前被检索和合并后已分离。实际上,hibernate还没有对集合的更改进行跟踪,因此会将其作为一个包进行管理似乎您没有正确地合并。是否从子集合中删除了任何内容?此外,我建议您使用适当的
hashCode()
和equals()
,如果可能,尝试使用Set
而不是List
。是的,这可能是您的合并问题。在更新前和更新后检查用户\u项目表,检查他们的组合值是否更改。此外,序列化所有实体。可能的重复项
Hibernate: select this_.code as code1_1_0_, this_.name as name2_1_0_ from country this_
Hibernate: insert into projects (besoins, countryCode, description, title) values (?, ?, ?, ?)
Hibernate: select last_insert_id()
Hibernate: update users set activated=?, email=?, lastError=?, password=?, profil_id =?, role=? where id=?
Hibernate: update profil set countryCode=?, countryName=?, description=?, firstname=?, lastName=?, phone=?, title=? where id=?
Hibernate: update projects set besoins=?, countryCode=?, description=?, title=? where id=?
Hibernate: update projects set besoins=?, countryCode=?, description=?, title=? where id=?
Hibernate: update projects set besoins=?, countryCode=?, description=?, title=? where id=?
Hibernate: update projects set besoins=?, countryCode=?, description=?, title=? where id=?
Hibernate: delete from users_projects where userid=?
Hibernate: insert into users_projects (userid, projectid) values (?, ?)
Hibernate: insert into users_projects (userid, projectid) values (?, ?)
Hibernate: insert into users_projects (userid, projectid) values (?, ?)
Hibernate: insert into users_projects (userid, projectid) values (?, ?)
Hibernate: insert into users_projects (userid, projectid) values (?, ?)