Bind two Entity without intermediate Entity spring data jpa

Tags: , , ,

I have a user table(and entity)

create table users(
  id                              number(9)          not null,
  alias                           varchar2(200 char),
  name_en                         varchar2(200 char),
  state                           varchar2(1)        not null

and user groups

create table user_groups(
  group_id                        number(9)          not null,
  alias                           varchar2(200)      not null,
  name_en                         varchar2(200 char),
  state                           varchar2(1)        not null,
  constraint user_groups1 primary key (group_id)

group users are stored in bind table

create table user_group_binds(
  group_id                        number(9)          not null,
  user_id                         number(9)          not null,
  constraint user_group_binds1 foreign key (group_id) references user_groups(group_id),
  constraint user_group_binds2 foreign key (user_id) references users(id)

I want to have in my group entity list of userEntity(without bind entity). Any ideas? Of course, I can use @Query annotation, but I have other entities who mapped with group and they will automatic get group entity. Can I override automatical methods?


You can use @JoinTable and define column mapping with join table in UserGroup Enitity

  @OneToMany(cascade = CascadeType.ALL)
  @JoinTable(name = "user_group_binds",
      joinColumns = {@JoinColumn(name = "group_id", referencedColumnName = "group_id")},
      inverseJoinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")})
  List<UserEntity> users;

Source: stackoverflow