users@glassfish.java.net

trouble with many-to-many table

From: <glassfish_at_javadesktop.org>
Date: Thu, 28 Feb 2008 08:02:06 PST

Ok. I have a table of games (GameT) and a table of users (UserT). There are two many-to-many relations between these tables.

A user may be the DM of a game.
A user may be Playing in a game.

If the user is dmming a game, then no additional information is needed. So I just use an anonymous many-to-many relation and it works *great*.

If the user is playing in a game, then I need extra info on the relation, so I have an additional table PlayerT. This table has its own generated primary key, and a pair of many-to-one joins (to UserT and GameT) matched by a pair of one-to-many joins on the other side.

Now, I am having all sorts of trouble creating a PlayerT record and deleting a PlayerT record.

When adding the player, do I create a new record, persist it, and then set the user and game on it and also add that new record to the sets in the user and game?

Do I create the new record, persist it, flush (so as get the database to generate a primary key), and then add it to the player sets in the user and game?

What's the actual sequence for creating and destroying many-to-many records of this form, where the PK is a generated one?
[Message sent by forum member 'pmurray_bigpond' (pmurray_bigpond)]

http://forums.java.net/jive/thread.jspa?messageID=261420