Spring Boot 在Sping Boot JDBC内使用在架构中创建的连接表

pdkcd3nj  于 2023-01-20  发布在  Spring
关注(0)|答案(1)|浏览(155)

我正在尝试使用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(),
...

我将如何用新的方法来定义和实现它呢?

igetnqfo

igetnqfo1#

我不太会说Kotlin,但我还是会尝试。请使用ChatGPT或个人经验来修复Kotlin错误;-)
第一步是识别聚合以及什么属于哪个聚合。我建议您有一个UserRole和一个User聚合,后者拥有关系。
这意味着您需要将关系添加到User中,User是User聚合的聚合根。由于Role是一个不同的聚合,因此您需要通过id引用它,并且需要一个单独的实体来保存该id

data class User ( 
...
    @MappedCollection(idColumn="user_id") 
    val roles: Set<RoleRef>
)
data class RoleRef(
    val roleId: AggregateReference<Role, Int>
)

如果希望从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

相关问题