In my spring boot application, I have an employe entity and a user entity , that are practically the same , just user has email and password so this entity is used only for authentication but this user references a specific employe so in database table user (id,email,password,employe_id) and employe(id,fname,lname,email,password,….,user_id) , and employe can be a manager or HR or just a simple employe so those are the role(id,role_name) and to associate each user with a role there is a join table user_role(user_id,role_id) , so this is my database structure now I am having issues when modifying information of an employee , changing the other fields goes normal but when it comes to changing role,which requires to delete a user then create a user with a different role then associate it with the modified employe :
this is the method that returns that error
@Transactional
public void modifyEmployee(Long id, String nom, String prenom,
String email, String motdepass
Departement departement, Long solde, String role) throws
EmployeExistException {
Employe employe = emp.findById(id)
.orElseThrow(() -> new NoSuchElementException("Employee not
found with ID: " + id));
// Update employee details
if (nom != null) employe.setNom(nom);
if (prenom != null) employe.setPrenom(prenom);
if (email != null) employe.setEmail(email);
if (motdepass != null) employe.setMotdepass(motdepass);
if (departement != null) employe.setDepartement(departement);
if (solde != null) employe.setSoldeconge(solde);
// Check if a role change is requested
if (role != null) {
User oldUser = employe.getUser();
if (oldUser != null) {
userRepository.delete(oldUser);
System.out.println("Deleted old user");
}
// Create a new user with the specified role
User newUser = createUserWithRole(employe, role);
employe.setUser(newUser);
System.out.println("Created new user with role: " + role);
}
// Save the modified employee
emp.save(employe);
}
User :
@Table(name = "users",
uniqueConstraints = {
@UniqueConstraint(columnNames = "username"),
@UniqueConstraint(columnNames = "email")
})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Size(max = 50)
@Email
private String email;
@NotBlank
@Size(max = 120)
private String password;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable( name = "user_roles",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Employe employe;
Employe :
public class Employe {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
protected String nom;
protected String prenom;
protected String email;
protected String motdepass;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "departement")
protected Departement departement;
protected Long soldeconge;
@OneToMany(mappedBy = "emp", cascade = CascadeType.ALL)
protected List<Demande> demandes;
@OneToMany(mappedBy = "emp", cascade = CascadeType.ALL)
protected List<Conge> conges;
protected LocalDate created_at;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;