0

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 ");

        }

1 Answer 1

1

When using Sping data JPA to update you should use save which you correctly did when saving on this line customerRepository.save(customer);. However when persisting data to a database in a PUT request JPA uses the keys within your entity mappings to be able to update the proper record.

So in your case you get that error when JPA tries to save a new record rather than an update to an existing record. Your intent is to update but I suspect your keys are missing or they are not properly defined so JPA tries to go and save a new record instead of updating.

So when you do the update(PUT) make sure the object you are passing has the same keys as the one you want to update.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer so you are saying the keys have to be exactly the same as the record i want to update. Let me try that and see if it will work then i will upvote .
thank you it worked, i was not adding the proper keys , so i just checked in postman and it worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.