0

My question is as follows: I am making a simple telegram bot where users can save and learn words in different languages (dictionary).I can't decide what kind of relationship between entities in a spring. I have two entities Word and User.

@Entity
@Table(name = "words")
@Data
public class Word {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String english;
    private String russian;
    @ManyToMany(mappedBy = "words")
    private Set<User> users;
}
@Entity
@Data
@Table(name = "users")
public class User {
    @Id
    private long id;
    private String firstName;
    @ManyToMany
    @JoinTable(
            name = "words_users",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "word_id"))
    private Set<Word> words;

The idea is to have a large dictionary in the database, and each user could add words to his dictionary, and the bot checks to see if he has the word in his dictionary, and if so, he gives a link to the word to the user, which is stored in the user's list, and if not, the word is added to the bot's dictionary and then to the user.

I used the "many to many" relationship, but I don't think that's the best option.I think that the intermediate table is completely out of place here, and the Word entity does not need to know which users are referring to which words. One to many(unidirectional) would be much better, but I can't find a suitable implementation. Does anyone have any ideas?

  • So you have to think about your relational model: if you wouldn't want to use an intermediate table it's ok but that would mean that words cannot be shared, so you will have duplicates (why? because if you don't have an intermediate table, you'll need a user_id column in your words table, making each row associated to a specific user) – Ale Zalazar Jan 05 '23 at 22:52

0 Answers0