Updating the Many in a JPA @OneToMany

We’re adding, updating and deleting data in v0.1.0 of my Spring-Data GitHub app. Schema recap is Contacts with a @OneToMany Contact Phones and a @ManyToMany Contact Hobbies relationship. Here we’re updating a Contact and its multiple Contact Phones. All of the code covered here is GitHub, in the v0.1.0 branch.

As shown in the console output below, our contact has multiple phone numbers and hobbies. We’re ignoring Hobbies in this update, to be covered later. We are limiting our specs to updating existing contact phone records only, not adding new ones or removing existing contact phone records. We’ll get to that later also.

Populating the ContactDTO

We first retrieve our Contact from the JPA Repository with a FindById(Long ContactId) and pass it to our contactToContactDTO() converter.

Updating the Contact and Its Contact Phones

We now have a populated ContactDTO object to pass to our Contact Service update() method. To update the multiple contact phones we iterate through them, retrieve each from the ContactPhoneRepository FindById(Long ContactPhoneId) and update the entities with the ContactPhoneDTO data.

 @Transactional(rollbackFor = NotFoundException.class)
    @Override
    public Contact update(ContactDTO updated) throws NotFoundException {
        LOGGER.info("Updating contact with information: {}", updated);

        Contact found = findById(updated.getContactId());

        //Update the contact information
        found.update(updated.getFirstName(), updated.getLastName(), updated.getEmail());
        //Update the contact phone
        if (found.getContactPhones() != null) {
            for (ContactPhoneDTO contactPhoneDTO : updated.getContactPhones()) {
                ContactPhone contactPhone = contactPhoneRepository.findByContactPhoneId(contactPhoneDTO.getContactPhoneId());
                if (contactPhone != null) {
                    contactPhone.update(contactPhoneDTO.getPhoneType(), contactPhoneDTO.getPhoneNumber());
                }
            }
        }
        return found;
    }

Testing the Contact and Multiple Phone Updates

There are four primary steps in confirming that both our Contact and its Contact Phones were updated. We retrieve the Contact and convert it to a ContactDTO shown above, which includes its Contact Phones.

1) We get the Contact’s first phone record, the Mobile contact phone, and confirm its last four digits end with “6511.” We’re going to update those to “9999.” We then change the contact’s last name from “Sullivan” to “Sullivananny.”

2) We update the Contact’s Mobile phone number with the last four digits “9999.”

3) We update the Contact in the Contact Service update() which we looked at earlier. This should update both the contact and any phone numbers modified.

4) Time to confirm the updates were applied and stored in the database. We retrieve the contact from the repository and convert it to a ContactDTO object. We can test the contact object itself, of course. Either way, we confirm that our database now stores the updated contact and the updated phone records.