Given the following mapping:
{code:java} @ElementCollection(fetch = FetchType.EAGER) private Set<String> phones;{code}
Removing an element when the entity is managed, will remove the whole collection.
Example:
{noformat} @Test public void removeElementAndAddNewOne(SessionFactoryScope scope) { scope.inTransaction( session -> { Person foundPerson = session.find( Person.class, thePerson.getId() ); Assertions.assertThat( foundPerson ).isNotNull(); foundPerson.getPhones().remove( "111-111-1111" ); foundPerson.getPhones().add( "000" ); assertThat( foundPerson.getPhones() ) .containsExactlyInAnyOrder( "999-999-9999", "123-456-7890", "000" ); } ); scope.inTransaction( session -> { Person person = session.find( Person.class, thePerson.getId() ); // Fails because it only contains "000" assertThat( person.getPhones() ) .containsExactlyInAnyOrder( "999-999-9999", "123-456-7890", "000" ); } ); }{noformat} |
|