I have a customer object and inside that customer object i have a login object which contains username and password. When i do a POST the request works fine however when i try to do a PUT request it fails. It fails because it says Duplicate entry on the username.
I would like to be able to update the customer details without having to change the username. How can i achieve this.
This is my code :
UserLogin Entity :
@Entity
@Table(name = "Customer",
uniqueConstraints =
{
@UniqueConstraint(columnNames = "email"),
@UniqueConstraint(columnNames = "id"),
@UniqueConstraint(columnNames = "phoneNumber")
}
)
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int customerNumber;
@OneToOne(cascade= CascadeType.ALL)
@JoinColumn(name = "loginCredentialsID")
private UserLogin userlogin;
private String phoneNumber;
private String email;
private String physicalAddress;
private String country;
... getters and setters
}
UserLogin Entity :
@Entity
@Table(name = "UserLogin",
uniqueConstraints =
{
@UniqueConstraint(columnNames = "userName")
})
public class UserLogin implements Serializable, UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int loginCredentialsID;
private String username;
private String password;
... getters and setters
}
CustomerService Class :
public Response updatCustomeretails(int id,Customer customer) {
customer.setCustomerNumber(id);
if( customer== null ){
throw new ResourceNotFoundException("Empty", "Missing Data");
}else {
customerRepository.save(customer);
return new Response(" Customer Updated Successfully","Thank you ");
}