在 Spring Boot 中执行 crud 操作时如何更新具有多对多关系的表
How to update tables with many-to-many relationship when performing crud operations in Spring Boot
我正在尝试为我的项目创建一个 Spring 引导后端。在数据库中,我有 Deck
和 Word
table 通过 DeckWord
table 连接的多对多关系。桥 table 有额外的字段和一个由其他 2 table 的 PK 组成的复合 PK。
我不确定我应该如何在我的项目中构造 crud 操作。假设我正在尝试添加一个新词,它应该分配给某个牌组。在那种情况下,什么模型的控制器应该处理 post
操作:Word
或 DeckWord
? Deck
的 List<DeckWord>
是否也应该更新?
更新:
包含模型,省略 getter、setter 和构造函数
@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;
}
和
@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;
}
和桥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;
}
回答您的问题:
在那种情况下,哪个模型的控制器应该处理 post 操作:Word 还是 DeckWord?
鉴于 Word
应该始终分配给 Deck
,那么我将使用 POST 请求 URL "/decks/{deckId} /words" 来创建一个新的 Word
。请求正文应包括 definition
和 transcription
.
卡组列表也应该更新吗?
是的,必须的。为此,您需要使用收到的 deckId
作为路径参数。
我正在尝试为我的项目创建一个 Spring 引导后端。在数据库中,我有 Deck
和 Word
table 通过 DeckWord
table 连接的多对多关系。桥 table 有额外的字段和一个由其他 2 table 的 PK 组成的复合 PK。
我不确定我应该如何在我的项目中构造 crud 操作。假设我正在尝试添加一个新词,它应该分配给某个牌组。在那种情况下,什么模型的控制器应该处理 post
操作:Word
或 DeckWord
? Deck
的 List<DeckWord>
是否也应该更新?
更新:
包含模型,省略 getter、setter 和构造函数
@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;
}
和
@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;
}
和桥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;
}
回答您的问题:
在那种情况下,哪个模型的控制器应该处理 post 操作:Word 还是 DeckWord?
鉴于 Word
应该始终分配给 Deck
,那么我将使用 POST 请求 URL "/decks/{deckId} /words" 来创建一个新的 Word
。请求正文应包括 definition
和 transcription
.
卡组列表也应该更新吗?
是的,必须的。为此,您需要使用收到的 deckId
作为路径参数。