英文:
Bind two Entity without intermediate Entity spring data jpa
问题
我有一个用户表(和实体)
create table users(
id number(9) not null,
alias varchar2(200 char),
name_en varchar2(200 char),
state varchar2(1) not null
);
以及用户组
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)
);
组用户存储在绑定表中
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)
);
我想要在我的组实体中拥有用户实体的列表(不包括绑定实体)。有任何想法吗?
当然,我可以使用@Query注释,但我有其他与组映射的实体,它们将自动获取组实体。
我可以覆盖自动生成的方法吗?
英文:
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?
答案1
得分: 1
您可以使用 @JoinTable
并在 UserGroup
实体中定义与联接表的列映射。
@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;
英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论