Using org.springframework.data.repository.CrudRepository, how can I add a many-to-many relationship to another exiting record with only the key reference to that entity.

For example:

If a person has a many to many relationship with addresses, how can I add to an existing person a relationship to an existing address by just using the address primary key.

1 Answers

1
Srinivasan Sekar On Best Solutions

Make sure to define relationships at pojo level , and use findBy or findOne to find the address object with primary key of address and set the address in person and call save ,

Address address = addressrepo.findOne(addressprimarykey);

person.setAddress(address);

personrepo.save(person);