users@glassfish.java.net

Accessing ids instead of objects in JPA _at_ManyToMany relationships

From: <glassfish_at_javadesktop.org>
Date: Fri, 05 Mar 2010 11:19:49 PST

Hi, I'm in the process of converting an application from making direct SQL calls to using JPA. I have two tables, "games" and "players" that now have entity beans and a many-to-many relationship between them. In the database, there is a separate table "game_players" (containing the columns "game_id" and "player_id") that maintains the relationship, and is referenced by the @ManyToMany annotation.

That's all quite nice, but a lot of my existing code just references the player_ids without needing to access the "players" table to get the whole "player" entity object every time.

For example, whenever a player tries to do something in the game, I first check his id against the set of player_ids for that game to make sure he's actually part of that game. In my old code all I have to do is query the "game_players" table and check that the player_id is in the returned list.

In my JPA code it looks as though I have to load all the player objects as well, before I can access their ids -- which would seem to add a lot of overhead to what was once a single database access operation.

I am new to JPA, so my question is, am I missing something? Is it possible to define the "game_players" relationship in such a way that I can avoid having to load all the data for each player from a different table when I just want to do a quick id check? I understand that the "game_players" table itself cannot be an entity, since there is no primary index, but beyond that I am a little stuck.

Thanks,

Mike
[Message sent by forum member 'tacitust' (tacitus_at_yahoo.com)]

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