Home > Blockchain >  How to update tables with many-to-many relationship when performing crud operations in Spring Boot
How to update tables with many-to-many relationship when performing crud operations in Spring Boot

Time:10-04

I'm trying to create a Spring Boot backend for my project. In the database I have Deck and Word tables with a many-to-many relationship connected via DeckWord table. The bridge table has additional fields and a composite PK consisting of the other 2 tables' PK's.

I am not sure about how I should structure the crud operations in my project. Say I'm trying to add a new word and it should be assigned to a certain deck. What model's controller should handle the post operation in that scenario: Word or DeckWord? Should the Deck's List<DeckWord> be updated as well?

UPDATE:

Included the models, omitted the getters, setters and constructors

@Entity
@Table(name = "deck")

    public class Deck {
        @Id
        @SequenceGenerator(
                name = "deck_sequence",
                sequenceName = "deck_sequence",
                allocationSize = 1
        )
        @GeneratedValue(
                strategy = GenerationType.SEQUENCE,
                generator = "deck_sequence"
        )
        @Column(name = "deck_id")
        private Long id;
        @Transient
        private Boolean learnt;
        private String name;
    
    
        @OneToMany(mappedBy = "deck", cascade = CascadeType.ALL)
        private List<DeckWord> deckwords;
    
        @ManyToOne
        @JoinColumn(name="appuser_id",referencedColumnName="appuser_id")
        private Appuser appuser;
    }

and

@Entity
@Table(name = "word")

    public class Word {
        @Id
        @SequenceGenerator(
                name = "word_sequence",
                sequenceName = "word_sequence",
                allocationSize = 1
        )
        @GeneratedValue(
                strategy = GenerationType.SEQUENCE,
                generator = "word_sequence"
        )
        @Column(name = "word_id")
        private Long id;
        private String definition;
        private String transcription;
    
    
        @OneToMany(mappedBy = "word", cascade = CascadeType.ALL)
        private List<DeckWord> deckwords;
    }

and the bridge table:

@Embeddable
class DeckWordKey implements Serializable {

    @Column(name = "deck_id")
    Long deckId;

    @Column(name = "word_id")
    Long wordId;
}


@Entity
@Table
public class DeckWord {
    @EmbeddedId
    DeckWordKey id;

    @ManyToOne
    @MapsId("deckId")
    @JoinColumn(name = "deck_id",referencedColumnName="deck_id")
    Deck deck;

    @ManyToOne
    @MapsId("wordId")
    @JoinColumn(name = "word_id",referencedColumnName="word_id")
    Word word;

    private Boolean learnt;
    private LocalDate last_checked;
    private WordGroup wordGroup;
}

CodePudding user response:

Answering your questions:

What model's controller should handle the post operation in that scenario: Word or DeckWord?

Given that a Word should always be assigned to a Deck, then I would use a POST request to the URL "/decks/{deckId}/words" to create a new Word. The request body should include definition and transcription.

Should the Deck's List be updated as well?

Yes, it must. For that, you need to use deckId that you receive as a path parameter.

  • Related