Dependent duplicate collections when saving an object

I have the following domain mapping:

@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(); } } 

and

 @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; //... } 

and the following code to execute:
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); ... } 

service:

 @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); } 

tao:

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

After that, I see that the administrator roles are tied to the user duplicated in the AdminRole table in the database.

How am I wrong? I wrote the equals method.

PS

before saving in debugging, I see the following values:

enter image description here

0
java spring-mvc hibernate
source share
1 answer

Since it is referenced in your new TerminalAdmin , AdminRole it does not contain an identifier. ID is an identifier of objects. It does not use equals() to identify identities. Without identifiers, Hibernate simply treats it as a new AdminRole to save to DB (since you set the appropriate cascade parameters in TerminalAdmin )

There are several options you can take.

  • Change your AdminRole to role or / and
  • Look at the correct AdminRole line on the role and set it to your new TerminalAdmin object or / and
  • Contains the AdminRole identifier in the incoming request, etc.
+1
source share

All Articles