如何在使用Spring Data JDBC处理多对多关系时处理元数据?

huangapple go评论70阅读模式
英文:

How to deal with metadata in a Many-To-Many relationship with Spring Data JDBC?

问题

我有一个项目,在这个项目中,bot_usergame_table 中进行游戏。因此,我有一个连接表。我还在该连接表中存储了 buy_in(玩家在桌子上可用的点数)。我的 SQL 代码如下:

CREATE TABLE bot_users (
    user_id bigint PRIMARY KEY,
    free_points bigint CHECK (free_points >= 0),
    frozen_points bigint CHECK (frozen_points >= 0)
);

CREATE TABLE game_tables (
    channel_id bigint PRIMARY KEY,
    owner bigint REFERENCES bot_users(user_id),
    in_game boolean NOT NULL
);

CREATE TABLE game_tables_participants (
    game_table_id bigint REFERENCES game_tables(channel_id),
    participant_id bigint REFERENCES bot_users(user_id),
    buy_in bigint NOT NULL,
    PRIMARY KEY (game_table_id, participant_id)
);

我的问题是:如何在 Java 实体中表示buy_in元数据?如果没有buy_in,我将简单地在GameTable实体中有一个ParticipantRef,然后有一些使用 ID 的方法。但我希望在代码中也能使用 buy_in,所以我应该创建一个类似于ParticipantRef的实体,其中包含buy_in吗?如果是的话,那么如何进行持久化?

(Note: The code you provided contains HTML-encoded characters for greater than and less than signs, which I've left as is in the translation.)

英文:

I have a project, where bot_users play in game_tables. So I have a join table. I also store the buy_in (points available to the player at the table) in that join table. My SQL:

CREATE TABLE bot_users (
    user_id bigint PRIMARY KEY,
    free_points bigint CHECK (free_points >= 0),
    frozen_points bigint CHECK (frozen_points >= 0)
);

CREATE TABLE game_tables (
    channel_id bigint PRIMARY KEY,
    owner bigint REFERENCES bot_users(user_id),
    in_game boolean NOT NULL
);

CREATE TABLE game_tables_participants (
    game_table_id bigint REFERENCES game_tables(channel_id),
    participant_id bigint REFERENCES bot_users(user_id),
    buy_in bigint NOT NULL,
    PRIMARY KEY (game_table_id, participant_id)
);

My question now is: how do I represent that buy_in metadata in entities in Java? If buy_in didn't exist, I would simply have a ParticipantRef of which I would have a Set in the GameTable entity, and then have methods that work on IDs there. But I want to have buy_in available in the code too, so should I create a ParticipantRef-like entity that contains the buy_in? If yes, then how would persisting it work?

答案1

得分: 0

你提出的解决方案基本上是完成此操作的方式。与其仅使用BotUsersuserId来拥有ParticipantRef,它还会获得一个buyIn属性。

由于您从GameTable通过SetParticipantRef进行了对象引用,它将被视为GameTable聚合的一部分,并且在持久化GameTable实例时也会被持久化。

英文:

Your proposed solution is pretty much the way to do this. Instead of having a ParticipantRef with just the userId of the BotUsers it also gets a buyIn attribute.

Since you have an object references from GameTable via a Set to the ParticipantRef it will be considered part of the GameTable-Aggregate and get persisted whenever you persist a GameTable instance.

huangapple
  • 本文由 发表于 2020年9月5日 06:12:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63748565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定