我正在尝试使用SQL模式预定义我的数据库结构,然后在我的Kotlin代码中使用它。
以下是我目前拥有的:
我的用户类:
data class User(
@Column(name = SqlQueries.Users.Entries.id) // "user_id"
val id: String?,
@Column(name = SqlQueries.Users.Entries.firstName) // "user_first_name"
var firstName: String = "Joe",
@Column(name = SqlQueries.Users.Entries.lastName) // "user_last_name"
var lastName: String = "Bloggs",
@Column(name = SqlQueries.Users.Entries.username) // "user_username"
var username: String = "${firstName}.${lastName}",
@Column(name = SqlQueries.Users.Entries.password) // "user_password"
@JsonIgnore
var password: String = "password1",
@Column(name = SqlQueries.Users.Entries.isActive) // "user_is_active"
val isActive: Boolean = true,
)
我的UserRole类:
data class UserRole(
@Column(name = SqlQueries.Lookups.UserRoles.Entries.id)
val id: Int? = null,
@Column(name = SqlQueries.Lookups.UserRoles.Entries.roleName)
val name: String = "",
)
我的架构:
-- ===================================================================================
-- Lookup Tables
-- ===================================================================================
-- Creates our User Table if one does not exist within the database already.
CREATE TABLE IF NOT EXISTS table_users(
user_id VARCHAR(60) DEFAULT RANDOM_UUID() UNIQUE PRIMARY KEY,
user_first_name VARCHAR NOT NULL,
user_last_name VARCHAR NOT NULL,
user_username VARCHAR NOT NULL UNIQUE,
user_password VARCHAR NOT NULL,
user_is_active VARCHAR NOT NULL
);
-- ===================================================================================
-- Lookup Tables
-- ===================================================================================
-- Creates our Roles Lookup Table if one does not already exist within the database.
CREATE TABLE IF NOT EXISTS lookup_roles(
role_id SMALLINT AUTO_INCREMENT UNIQUE PRIMARY KEY,
role_name VARCHAR NOT NULL UNIQUE
);
-- ===================================================================================
-- Junction Tables
-- ===================================================================================
-- Creates our User/Role Junction table if one does not already exist within the database.
-- This will be the foundation of a many to many relationship between the two entities.
CREATE TABLE IF NOT EXISTS junction_user_role(
user_id VARCHAR(60),
role_id SMALLINT,
CONSTRAINT pk_user_role PRIMARY KEY (user_id, role_id),
CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES table_users (user_id),
CONSTRAINT fk_role FOREIGN KEY (role_id) REFERENCES lookup_roles (role_id)
);
正如您所看到的,我创建了一个User、Role和User/Role表,它被设计为具有多对多关系。
我对能够将关系作为一个变量"访问"并在以后使用感到一无所知。
在我以前的"原型"中,我使用了以下格式,它有不同的设计概念:
用户类别:
...
@ManyToMany(fetch = FetchType.EAGER)
@Column(name = "user_roles")
var userRoles: MutableSet<Role> = mutableSetOf(),
...
我将如何用新的方法来定义和实现它呢?
1条答案
按热度按时间igetnqfo1#
我不太会说Kotlin,但我还是会尝试。请使用ChatGPT或个人经验来修复Kotlin错误;-)
第一步是识别聚合以及什么属于哪个聚合。我建议您有一个UserRole和一个User聚合,后者拥有关系。
这意味着您需要将关系添加到
User
中,User
是User聚合的聚合根。由于Role是一个不同的聚合,因此您需要通过id引用它,并且需要一个单独的实体来保存该id如果希望从
UserRole
导航到所有具有该角色的User
实体,则需要为此在UserRepository
中创建一个repository方法。https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates中详细介绍了其工作原理
还涉及:Spring Data JDBC many to many relationship management