I've got a unidirectional OneToMany relationship that I'd like to do an outer join on. However, the query results aren't exactly correct unless I change the table structure a bit.
As an example lets use the entities Team and Player. On the team entity we'll include the following mapping to player:
@OneToMany() public Set<Player> getPlayers() { return players; } public void setPlayers(Set<Player> players) { this.players = players }
The current configuration should result in three tables: TEAM, TEAM_PLAYER, and PLAYER
Now lets say I want to run the following HQL query: SELECT t.name, p.name FROM Team t LEFT OUTER JOIN t.players p WITH p.salary > 100000
The resultant SQL will be something like: select t.name, p.name from TEAM t left outer join TEAM_PLAYER tp ON tp.TEAM_ID = t.ID left outer join PLAYER p on tp.PLAYER_ID = p.ID and p.salary > 100000
This will perform a join to every player on the team regardless of what their salary is. This is because of the way it splits the single HQL outer join (t.players) into two SQL outer joins (TEAM_PLAYER and PLAYER).
There is a workaround to this problem by adding the @JOIN_COLUMN annotation so that a TEAM_ID column will be placed on the player table instead of creating the intermediary TEAM_PLAYER table. However, this solution causes the database to no longer be normalized. Keep in mind that this is just a simplified example to get my point across and that my real world example is far more complex. What do you think? Should I file a bug for this issue? Thanks for your help.
|