How to create join table with JPA annotations?

后端 未结 4 1296
深忆病人
深忆病人 2020-12-04 10:12

I need to create a join table in my database using JPA annotations so the result will be this:

\"ent

4条回答
  •  [愿得一人]
    2020-12-04 10:55

    I would implement it this way:

    @Entity
    @Table(name="GROUPS", schema="ADMIN")
    public class Group implements Serializable {
      @OneToMany
      @JoinTable(name = "USER_GROUP",
                joinColumns = @JoinColumn(name = "groupid"),
                inverseJoinColumns = @JoinColumn(name = "userid"))
      private List users;
    }
    

    Solution suggested by @PedroKowalski should work too, but then you'll have to keep a reference to Group entity in your User entity which is not always possible.

提交回复
热议问题