-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: @ManyToMany fetch problem.
PostPosted: Wed Apr 13, 2011 6:56 am 
Newbie

Joined: Wed Apr 13, 2011 6:37 am
Posts: 4
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.


Top
 Profile  
 
 Post subject: Re: @ManyToMany fetch problem.
PostPosted: Wed Apr 13, 2011 11:21 am 
Newbie

Joined: Tue Sep 14, 2010 4:29 pm
Posts: 16
Hi Extacier,
I see two possible reasons for Hibernate's behaviour:
[list=]
[*]The filter you use on the team property. I am not familiar with filters, but you should check that.
[*]You use a Set of Teams. Sets ensure uniqueness on the Java-side by checking either equals() or compareTo() (depending on the underlying implementation). Check those methods in your Team entity for consistency and for returning the correct values fon non equal entities (false, !=0).
[/list]

Greetz ngomo

P.S. Sorry for the ugly list. I seem to use the forum's list feature in a wrong way...

_________________
http://www.winfonet.eu


Top
 Profile  
 
 Post subject: Re: @ManyToMany fetch problem.
PostPosted: Thu Apr 14, 2011 4:27 am 
Newbie

Joined: Wed Apr 13, 2011 6:37 am
Posts: 4
I have solved the problem out.
It was caused by a hsql query which was run before the given query.
In the before query, players' teams fetched only by given team and players cached.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.