英文:
How to deal with metadata in a Many-To-Many relationship with Spring Data JDBC?
问题
我有一个项目,在这个项目中,bot_user
在 game_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_user
s play in game_table
s. 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
你提出的解决方案基本上是完成此操作的方式。与其仅使用BotUsers
的userId
来拥有ParticipantRef
,它还会获得一个buyIn
属性。
由于您从GameTable
通过Set
对ParticipantRef
进行了对象引用,它将被视为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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论