'Spring Data JDBC many to many relationship management

I have a many-to-many relationship person -> person_address <- address and use a reference class. But in my Person aggregate root it seems only adding person_address works (addresses collection):

@MappedCollection(idColumn = "PERSON_ID")
private Set<PersonAddress> addresses;

public void addAddress(final Address address) {
    Assert.notNull(getId(),"Person ID cannot be null");
    Assert.notNull(address.getId(),"Address ID cannot be null");
    addresses.add(new PersonAddress(address.getId()));
}

I want to be able to delete from addresses collection and then do a save, but this doesn't work. So instead I use:

@Modifying
@Query(value = "delete from person_address where person_id = :personId and address_id = :addressId")
void deletePersonAddressById(@Param("personId") final Long personId, final Long addressId);

Is this the best way to handle this?

@ToString
@EqualsAndHashCode
@Getter
@Setter
public class PersonAddress {

    /**
     * Timestamp generated by database.
     */
    @ReadOnlyProperty
    @Setter(AccessLevel.NONE)
    private LocalDateTime created;

    private Long addressId;

    public PersonAddress(final Long addressId) {
        this.addressId = addressId;
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source