Spring Data R2dbc中处理表之间关系的最佳实践 [英] Best practice of handling relations between tables in Spring Data R2dbc
问题描述
我尝试在 RDBMS 中创建用户/角色关系,并想使用 R2dbc(Spring Data R2dbc) 与后端数据库握手.
I tried to create a user/roles relation in RDBMS and want to use R2dbc(Spring Data R2dbc) to shake hands with the backend database.
假设有三个表,users、roles 和 user_roles.
Assume there are three tables, users, roles, and user_roles.
@Table("users")
class User {
@Id
private String username;
private String password;
private String email;
@Builder.Default
private boolean active = true;
@Builder.Default
private List<String> roles = new ArrayList<>();
@Column("created_at")
private LocalDateTime createdDate;
}
与 JPA 不同,R2dbc 重用 spring-data-relational-common(在 Spring Data Jdbc 中也使用)来注释表,但没有解决关系的工具,例如 roles代码>在这里.
Unlike JPA, R2dbc reuses the spring-data-relational-common(which is also used in Spring Data Jdbc) to annotate the tables, but there is no facility to resolve the relations, such as the roles
here.
推荐答案
Spring Data R2DBC 目前不支持关系.
Spring Data R2DBC currently does not support relationships.
所以你要做的是有一个单独的实体 User2Role
有两个属性:String username
和 String rolename
引用引用的实体.
So what you would do is to have a separate entity User2Role
with two properties: String username
and String rolename
referencing the ids of the referenced entities.
因为您还标记了问题 Spring Data JDBC:Spring Data JDBC 确实支持 1:1 和 1:M 引用,但不支持 M:1 或 M:N 关系.参见 https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates 了解相关背景.
Since you also tagged the question Spring Data JDBC: Spring Data JDBC does support 1:1 and 1:M references, but not M:1 or M:N relationships. See https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates for some background on that.
Spring Data R2DBC 最终可能会迁移到相同的模型.
Spring Data R2DBC might eventually move to the same model.
这篇关于Spring Data R2dbc中处理表之间关系的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!