0

I have my entity as shown below

@Entity
@Table(name = "Contact")
@Data
public class Contact{

    Long Id;

    String phoneNumber;

    @javax.validation.constraints.Email(message = "A valid email address is required")
    private String emailAddress;

    String type;
}

I am updating the type for the existing record from Contact table

existing record

{
    "phoneNumber":"1234567890";
    "emailAddress":"testEamail"
    "type":"1"
}

I am changing type to 2 with PATCHrequest. But I am getting error "A valid email address is required" as existing email is in valid format. I can't change existing emailAddress.

Any idea how can I skip javax.validation during update/PATCH

I am getting an error when we save existing contact in service class

service class code

Contact contact = contactRepository.getById(id);
contact.setType(2);
contactRepository.save(contact);
SSK
  • 3,444
  • 6
  • 32
  • 59
  • but you do want to update/patch the `emailAddress` field with the new invalid value? – areus May 05 '20 at 08:42
  • I can't update/change email. I only update for type – SSK May 05 '20 at 08:58
  • Can you please provide code on how you update your `Contact#type` ? Also I'm unsure if I understand your question correctly, are you saying you have an invalid entry in your database or are you saying you are saving and invalid entry? Why can't you just retrieve the entity from the database, change the type and save it again, or are you doing that? – PaulD May 05 '20 at 09:14
  • why do you want to update the Email to an invalid value? If it is valid change your validation annotations to reflect the actual rules. – Jens Schauder May 05 '20 at 13:47
  • I am not updating the email it's existing data. data is shared with different applications – SSK May 05 '20 at 13:49
  • So you are saying the existing entry of `Contact` has an invalid email saved? If not, why would the validation fail? – PaulD May 06 '20 at 08:58
  • Yes, existing email is invalid. – SSK May 06 '20 at 09:53

1 Answers1

1

Validation annotations have a group parameter which allows to assign them to validation groups.

Bean Validation and JPA integrate nicely:

The JPA specification defines a set of configuration parameters to configure which ValidationGroups shall be validated before performing an insert, update, and remove operations.

favax.persistence.validation.group.pre-persist
javax.persistence.validation.group.pre-update
javax.persistence.validation.group.pre-remove

See:

Lesiak
  • 22,088
  • 2
  • 41
  • 65