gpt4 book ai didi

java - 当我保存实体时,从属集合会重复

转载 作者:行者123 更新时间:2023-12-01 11:00:22 25 4
gpt4 key购买 nike

我有以下域映射:

@Entity
@Table(name = "terminal_admin_role")
public class AdminRole {

@Id
@Column(name = "role_id", nullable = false, unique = true)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id")
@SequenceGenerator(name = "user_id", sequenceName = "user_id")
private Long adminId;

@Column(name = "role")
private String role;

public AdminRole(String role) {
this.role = role;
}

public AdminRole() {
}

// get set

@Override
public String toString(){
return role;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof AdminRole)) {
return false;
}

AdminRole adminRole = (AdminRole) o;

if (!role.equals(adminRole.role)) {
return false;
}

return true;
}

@Override
public int hashCode() {
return role.hashCode();
}
}

@Entity
@Table(name = "terminal_admin")
public class TerminalAdmin {
@ManyToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JoinTable(name = "admin_role", joinColumns = {
@JoinColumn(name = "admin_id", nullable = false) },
inverseJoinColumns = { @JoinColumn(name = "role_id",
nullable = false) })
private Set<AdminRole> adminRoles;
//...
}

并执行以下代码:
Controller :

@RequestMapping(value = "/admin/addNewAdmin")
public String adminUsers(@ModelAttribute @Valid TerminalAdmin terminalAdmin,
BindingResult bindingResult, ModelMap model, Principal principal, HttpSession session) {
...
terminalAdmin.setCreateDate(Calendar.getInstance());
terminalAdminService.saveTerminalAdmin(terminalAdmin);
...
}

服务:

@Override
@Transactional
public void saveTerminalAdmin(TerminalAdmin newAdmin) {
String rawPassword = newAdmin.getPassword();
newAdmin.setPassword(passwordEncoder.encode(newAdmin.getPassword()));
terminalAdminDao.save(newAdmin);
emailService.sendAdminCreatedEmail(rawPassword, newAdmin.getEmail(), newAdmin.getAdminRoles());
emailService.sendAdminRegisteredForAdminEmail(newAdmin);
}

道:

@Override
@Transactional
public void save(TerminalAdmin terminalAdmin) {
sessionFactory.getCurrentSession().save(terminalAdmin);
}

之后,我看到绑定(bind)到用户的管理员角色在数据库的 AdminRole 表中重复。

我做错了什么?我写了 equals 方法。

附注

在调试中保存之前,我看到以下值:

enter image description here

最佳答案

因为在新的 TerminalAdmin 中,它引用的 AdminRole 不包含 ID。 ID是实体的身份。它不使用equals()来识别身份。如果没有 ID,Hibernate 只是将其视为新的 AdminRole 来持久化到数据库(因为您已在 TerminalAdmin 中设置了相应的级联选项)

您可以做出一些选择

  1. 将您的 AdminRole ID 更改为 role 字符串,或/和
  2. 通过 role 字符串查找正确的 AdminRole,并将其设置在新的 TerminalAdmin 实体中,或/和
  3. 包含传入请求中的AdminRole ID等...

关于java - 当我保存实体时,从属集合会重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33384989/

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