Search code examples
spring-data-jdbc

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;
    }
}

Solution

  • You should be able to just remove entries from Person.addresses and save the entity again.

    I created a sample to demonstrate this on GitHub.

    On pitfall I fell into in the past was to not properly implement equals and hashCode for PersonAddress, which is of course important, when you want to remove instances from a HashSet or similar.