Spring JPA data validation missing during upgrade (merge)

I am working on a project with Spring boot 1.5.4.RELEASE with Spring JPA data.

A question has been fixed that the Hibernate validator is not executed when the object is updated, or at least in some cases it does not check.

For Person , as shown below, it is forbidden to have empty names, and the skill set must have a minimum of 1 element and a maximum of 5. Both of them are checked during a call to save to the Spring data store. However, to save the call to an existing object, he will only check the name constraints - and will not test the skills.

@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @NotBlank
    private String name;

    @ElementCollection(fetch = FetchType.EAGER)
    @Size(min = 1, max = 5)
    private Set<String> skills = new HashSet<>();

    protected Person() {
    }

    public Person(final String name, final Collection<String> skills) {
    this.name = name;
    this.skills.addAll(skills);
    }

    public void updateSkills(Collection<String> skills) {
    this.skills.clear();
    this.skills.addAll(skills);
    }

    public void updateName(final String name) {
    this.name = name;
    }
    //getters
}

and when creating a new object with an empty skills list, it will throw a ConstraintValidationException as expected:

@Test(expected = ConstraintViolationException.class)
public void shouldFailWhenSkillsAreEmpty() {
    //given
    Person person = new Person("gucio", Collections.EMPTY_LIST);
    //when
    personRepository.save(person);
    //then
}

https://github.com/konczak/demo-skills-validation-issue/blob/master/src/test/java/com/example/demo/entity/PersonTest.java#L80

- (merge underhood? not sure) , :

@Test(expected = ConstraintViolationException.class)
public void shouldFailWhenUpdateSkillsToEmpty() {
    //given
    Person person = new Person("gucio", Arrays.asList(JAVA, SQL));
    person = personRepository.save(person);
    person.updateSkills(Collections.EMPTY_LIST);
    //when
    personRepository.save(person);
    //then
}

https://github.com/konczak/demo-skills-validation-issue/blob/master/src/test/java/com/example/demo/entity/PersonTest.java#L104

, , , 1 ConstraintViolation :

https://github.com/konczak/demo-skills-validation-issue/blob/master/src/test/java/com/example/demo/entity/PersonTest.java#L119

:

https://github.com/konczak/demo-skills-validation-issue

, / , /. , - , ConstraingViolationException.

https://github.com/konczak/demo-skills-validation-issue/blob/master/src/test/java/com/example/demo/entity/PersonTest.java#L91

?

+1

Source: https://habr.com/ru/post/1687890/


All Articles