gpt4 book ai didi

java - 如何使用 hibernate 更新实体

转载 作者:搜寻专家 更新时间:2023-11-01 03:47:54 25 4
gpt4 key购买 nike

我有实体用户:

@Entity
@Table(name = "entity_user")
@AttributeOverride(name = "id", column = @Column(name = "user_id"))
public class User extends BaseObject implements Serializable {

/**
* Login, unique
*/
private String email;

private String username;

/**
* Secret for signing-in
*/
private String password;

/**
* Type of user
*/
private UserType userType;

@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinTable(name = "view_user_to_requestSet", joinColumns = {
@JoinColumn(name = "user_id")}, inverseJoinColumns = {
@JoinColumn(name = "requestSet_id")})
private Set<RequestSet> setOfRequesSet = new HashSet<>();

@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinTable(name = "view_user_to_requestSetCreator", joinColumns = {
@JoinColumn(name = "user_id")}, inverseJoinColumns = {
@JoinColumn(name = "requestSet_id")})
private Set<RequestSet> setOfRequesSetCreator = new HashSet<>();

@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinTable(name = "view_user_to_userTask", joinColumns = {
@JoinColumn(name = "user_id")}, inverseJoinColumns = {
@JoinColumn(name = "userTask_id")})
private Set<UserTask> setOfUserTask = new HashSet<>();

public User() {
}

.
.
..... GET AND SET

基础对象是:@MappedSuperclass公共(public)类 BaseObject {

@Id
@GeneratedValue
@Column(name = "entity_id")
private Long id;

/**
*
* @return true if the entity hasn't been persisted yet
*/
@Transient
public boolean isNew() {
return id == null;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

问题是,当我尝试更新对象时,出现错误:

Exception in thread "Thread-15" org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [UK_5fa729oayn43ynq4v2y4d9qcn]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '5' for key 'UK_5fa729oayn43ynq4v2y4d9qcn'

对于我正在使用的更新实体:

@Transactional
public void save(User value){
em.merge(value);
}

我不知道为什么会出现此错误,我该如何更新我的对象?感谢您的帮助。

编辑:我更新:

User u = em.find(persistedType, id); //get user by ID
u.getSetOfRequesSet().add(requestSet); //add one otem to set
userDap.merge(u); //save

最佳答案

看起来您的更新重复了集合 (em.merge) 中的条目。为防止重复,您应该明确合并集。按如下操作。

  1. 向用户添加方法合并:

    protected User merge(User other) {
    // Just assign all data members(2 examples here)
    setEmail(other.getEmail());
    ....
    setSetOfRequesSet(other.getSetOfRequesSet());
    ...
    }
  2. 准备更新的对象实例:

    User updated = em.find(persistedType, id); //get user by ID
    updated.getSetOfRequesSet().add(requestSet); //add one item to set
  3. 更新现有的数据库实体:

    User existing = em.find(persistedType, id);
    existing.merge(updated);

更新后不需要调用 em.merge,因为保存将在事务结束时完成。

你可以找到解释here .

关于java - 如何使用 hibernate 更新实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38930631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com