请帮助我设计 Spring Boot 应用程序的架构。我想创建一个字典,在其中可以放置一些带有转录、翻译和同义词集的单词。所以问题是:我有一个单词实体,每个单词都可以有一组同义词,但我无法理解如何在单词和同义词之间创建关系,因为同义词本身也是一个单词。我无法在 Word 实体中定义 Set 同义词。那么我该如何实施呢? 我尝试过,但我知道这是愚蠢的方式:
@Table(name = "Word")
public class Word {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@NotBlank(message = "You should add some word here!")
@Column(name = "spelling")
private String spelling;
@Column(name = "transcription")
private String transcription;
@NotBlank(message = "You should add translation here!")
@Column(name = "translation")
private String translation;
@ManyToOne
@JoinColumn(name = "synonyms_id", referencedColumnName = "id")
private Synonyms synonyms;
@ManyToOne
@JoinColumn(name = "dictionary_id", referencedColumnName = "id")
private Dictionary dictionary;
@Table(name = "Synonyms")
public class Synonyms {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@OneToMany(mappedBy = "synonyms", fetch = FetchType.LAZY)
private Set<Word> words;
I think according to your use-case if a word is also a synonymsthen you don't need another entity to make relationship
and you can create a many to many relationship means a word can be associated with synonyms and also a word can be synonyms of other words just like this
@ManyToMany
@JoinTable(
name = "word_synonyms",
joinColumns = @JoinColumn(name = "word_id"),
inverseJoinColumns = @JoinColumn(name = "synonym_id")
)
private Set<Word> synonyms = new HashSet<>();
@ManyToMany(mappedBy = "synonyms")
private Set<Word> synonymousOf = new HashSet<>();