I have a class that needs to manage a mapping of two other classes, but I need to isolate it to only the records it cares about. Basically, you have a player who might have an account on any number of game servers. Each server has to manager the player-to-account mapping for only its server. In these stub classes, I'm omitting a lot of unnecessary fields. Assume
// player has no collections or associations in the class
public class Player {
@Id @GeneratedValue @Column private int id;
// other crap about the actual person
}
// character has an association to the player and the server
public class LocalCharacter {
@Id @GeneratedValue @Column private int id;
@ManyToOne private Player player;
@ManyToOne private GameServer server;
// other crap about this person's character on this server
}
// game server needs to know who all is on it, but it needs to be mapped to the player
public class GameServer {
@Id @GeneratedValue @Column private int id;
@/* no idea here */ private Map<Player, LocalCharacter> localCharacters;
}
I'm not sure how to construct this mapping. I know if I just wanted a Set<LocalCharacter>
I could do that with an @OneToMany
. I know I could do a join fetch
on the player and construct the Map myself, but that seems lame - I'd like hibernate to do it for me! :-) How can I make this happen?