I have two entities called Team and Player.
Code:
@Entity
@Table(name = "ALT_TEAM")
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Filters( { @Filter(name = "CUSTOMER_FILTER"), @Filter(name = "ENTITY_FILTER"), @Filter(name = "BRANCH_FILTER"), @Filter(name = "SEASON_FILTER") })
public class Team{
private Set<Player> players=new HashSet<Player>(0);
@ManyToMany(fetch = FetchType.LAZY,cascade=CascadeType.MERGE,mappedBy="teams")
@Filter(name = "ENTITY_FILTER")
public Set<Player> getPlayers(){
return players;
}
public void setPlayer(Set<Player> players){
this.players=players;
}
}
Code:
@Entity
@Table(name = "ALT_PLAYER")
@Filters( { @Filter(name = "CUSTOMER_FILTER"), @Filter(name = "ENTITY_FILTER"), @Filter(name = "BRANCH_FILTER") })
public class Player{
private Set<Team> teams=new HashSet<Team>(0);
@ManyToMany(fetch = FetchType.LAZY,cascade=CascadeType.MERGE)
@JoinTable(name = "ALT_TEAM_PLAYER",
joinColumns = @JoinColumn(name = "PLAYER_ID", nullable = false),
inverseJoinColumns = @JoinColumn(name = "TEAM_ID", nullable = false))
@Filter(name = "ENTITY_FILTER")
public Set<Team> getTeams(){
return players;
}
public void setPlayer(Set<Team> teams){
this.teams=teams;
}
}
When I run the hsql query
Code:
SELECT player FROM Player player LEFT JOIN FETCH player.teams
only the last team is fetched in the player who have teams for more than one season.
The generated query seems has no problem, it selects all teams but hibernate eliminate the teams for a reason which I couldn't figure out.
This problem occurs not only whereas in-query fetching, also lazy fetching.