#Deleting from many to many entity

1 messages · Page 1 of 1 (latest)

hardy path
#

I am trying to delete from an many to many entity which is cascade.all but its not deleting ! i am confused ```@Transactional
public boolean deleteInputAndOutput(long id) {
// Get the current authenticated user
CustomUserDetails customUserDetails = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User currentUser = customUserDetails.getUser();

// Retrieve the InputAndOutput entity by ID, or throw an exception if not found
InputAndOutput inputAndOutput = inputAndOutputRepository.findById(id)
    .orElseThrow(() -> new NoInputAndOutputFound("This input or output is not present in the db " + id));

// Check if the authenticated user is associated with this InputAndOutput
if (inputAndOutput.getUsers().contains(currentUser)) {
    // Remove the InputAndOutput from the user's inputAndOutputs list
    currentUser.getInputAndOutputs().remove(inputAndOutput);

    // Remove the user from the InputAndOutput's users list
    inputAndOutput.getUsers().remove(currentUser);

    // Save the updated user (removes the association)
    userRepository.save(currentUser);

    // Optionally, delete the InputAndOutput if no users are left
    if (inputAndOutput.getUsers().isEmpty()) {
        inputAndOutputRepository.delete(inputAndOutput);
    }

    return true;
} else {
    // If the authenticated user is not associated with this InputAndOutput, return false
    return false;
}

}```

coarse tulipBOT
#

<@&1004656351647117403> please have a look, thanks.

coarse tulipBOT
#

@hardy path

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

hardy path
#

Did any one know about it ?

surreal siren
#

is bi-directional mapping?

north jetty
#

Are you sure that those are the same entities?

#

I would rather use an Iterator and check by id

stuck geode
#

Check this article, goes into a few things and subtleties you might have forgotten