0

I have to entities modeled Session and Speaker, with ManyToMany relationship, and I wanted to delete an instance of Session, but in the DB it is the foreign key of another table. Below is the entity model

@Entity(name = "sessions")
public class Session {
    // attributes do not respect camel case notations because they
    // need to match table notations in order to auto bind without annotations
    // otherwise that is done with @Column annotation
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long session_id;
    private String session_name;
    private String session_description;
    private String session_length;
    @OnDelete(action = OnDeleteAction.CASCADE)
    @ManyToMany()
    @JoinTable(
            name = "session_speakers",
            joinColumns = @JoinColumn(name = "session_id"),
            inverseJoinColumns = @JoinColumn(name = "speaker_id")
    )
    private List<Speaker> speakers;

    public Session() {
    }

I tried to use OnDelete Cascade, but it still didn't work. (I did read that it is not advised to use on ManyToMany relationship)

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)

    public void delete(@PathVariable Long id){
        sessionRepo.deleteById(id);
    }

EDIT:

here is also the Speaker entity

@Entity(name = "speakers")
public class Speaker {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long speaker_id;

    private String first_name;
    private String last_name;
    private String title;
    private String company;
    private String speaker_bio;

    @Lob
    @Type(type = "org.hibernate.type.BinaryType")
    private Byte[] speaker_photo;

    public Byte[] getSpeaker_photo() {
        return speaker_photo;
    }

    public void setSpeaker_photo(Byte[] speaker_photo) {
        this.speaker_photo = speaker_photo;
    }

    @ManyToMany(mappedBy = "speakers")
    @JsonIgnore// added to resolve serialization issues
    private List<Session> sessions;
Alex
  • 3
  • 2
  • You should read https://stackoverflow.com/questions/1082095/how-to-remove-entity-with-manytomany-relationship-in-jpa-and-corresponding-join and post the source code of Speaker entity. – p3consulting Jan 08 '23 at 09:54
  • @p3consulting I will look over it. I also added the speaker entity – Alex Jan 08 '23 at 13:48
  • @p3consulting "Let say the User is the owner. Then when deleting a user the relation user-group will be updated automatically" from the First answer. So in my case it should be fine, no? Since session is the owner – Alex Jan 08 '23 at 14:10
  • When you delete a Session you should notify its Speakers to remove it from their list of Session. – p3consulting Jan 08 '23 at 14:21
  • @p3consulting in my DB I also have "session_id" field as a PK for another table, for "session_schedule". Isn't that also an issue? – Alex Jan 08 '23 at 14:50
  • Yes: depending on how you manage the relationship... and how you define the cascading of the DELETE – p3consulting Jan 08 '23 at 14:55

0 Answers0