简体   繁体   中英

Hibernate: how to set relationship among three entity classes, their join table and save in database

I have following three entity classes.

    @Entity
    public class User {

        @Id
        @Column(nullable = false)
        @GeneratedValue(strategy= GenerationType.AUTO)
        private Integer id;
    }

@Entity
public class LanguageProficiencyLevel {

    @Id
    @Column(nullable = false)
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Integer id;

    private String name; // A1, A2, B1 ... etc
}

@Entity
public class Language {

    @Id
    @Column(nullable = false)
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Integer id;

    private String name; //English, Chinese ect ...
}

Currently in the database, I have around 20 languages saved in Language table and 6 language proficiency levels A1, A2, B1, B2, C1, C2 saved in LanguageProficiencyLevel table.

Now I have the following relationship among the entity classes.

A User can know more than one languages with one proficiency level and A language with one proficiency level is known by many users.

So for example, A user may know English and his English proficiency may be C1, Again same user may also know Spanish and his Spanish proficiency may be B1.

Here I understand, User and Language has many to many relation. But I don't understand how to relate LanguageProficiencyLevel with User or Language.

Also how should I save this in database? My idea is to make one join table (LanguageSkill) with column names as user_id, language_id and languageProficiencyLevel_id and this table row will be inserted when a user is created. I am not sure if this the way to implement it. Please give me an idea how to do this and what should be the configuration for this.

User and Language will have many to many relation as you said. And, Language and LanguageProficiencyLevel will have one to one relation.

So, you have to create a mapping table that will have have many to one relation to all the 3 tables. Refer this link to create mapping table with multiple columns.

Your relations between entity objects would be like:

  • A user can have multiple languages and language can have multiple users. So its a Many-to-Many relationship between User-Language.
  • A language can have only one proficiency at a time but a proficiency can have multiple languages. So LanguageProficiency to Language would be a one-to-many relation.
  • Relation between user and language proficiency is also many-to-many.

Here is a link how you can go about your database design for many-to-many relations. How to implement a many-to-many relationship in PostgreSQL?

After creating the database design you can probably use some reverse engineering tool( https://www.javacodegeeks.com/2013/10/step-by-step-auto-code-generation-for-pojo-domain-java-classes-and-hbm-using-eclipse-hibernate-plugin.html ) to create the hibernate pojo classes. I would recommend to use a tool to do this rather than taking things in hands to avoid unnecessary issues.

So now if you look carefully... your Entity classes of User, Language and LanguageProficiency would be something like this.

Hope this is useful.

You should absolutely create another table LanguageSkill, like you said.

Language and Proficiency are so-called base data - they will have comparatively few entries and will be independent of users. Neither of them should be mapped into User.

A User should then have a @OneToMany relation to LanguageSkill, which represents his knowledge of a particular language. LanguageSkill has a @ManyToOne to both Language and Proficiency (and User).

Skipping LanguageSkill would result in data duplication in your schema, or at least in a schema that is hard to read with all the jointables. Also, it would mix concerns - data that is relatively stable (Language, Proficiency) and data that will change often (a person's knowledge of a language).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM