2011-04-18 157 views
0

我有以下3類,其中的關係是聯賽1 * 1隊球員*和 聯賽和團隊由league_team錶鏈接休眠 - 雙左連接

class League { 

@NamedQueries({ @NamedQuery(name = "League.findJasperLeagueDetail", query = "from League r " 
     + "where league_id = :Id") }) 
//getter setter methods 
... 

private Set<Team> team; 

@OneToMany(cascade = CascadeType.ALL) 
@JoinTable(name="league_team",[email protected](name="team_id"), 
[email protected](name="league_id")) 

public Set<Team> getTeams() { 
    return team; 
    } 
} 


class Team { 
private Set<Player> players; 

@OneToMany(mappedBy="teams") 
public Set<Player> getPlayers(){ 
     return this.players; 
    } 
} 

class Player{ 

private Team team; 

@ManyToOne 
@JoinColumn(name="team_id") 
public Team getTeam(){ 
     return this.team; 
} 
} 


public static void main (String[] ag){ 

// Get the entity manager em 

     League league = em.find(League.class, 52L); 
    Query query = em.createNamedQuery("League.findJasperLeagueDetail"); 
    query.setParameter("leagueId", league.getId()); 


    List<League> result = query.getResultList(); 
     Set teams = result.get(0).getReports(); 
     Iterator<Team> it = teams.iterator(); 
     if(it.hasNext()){ 
      Set<Player> players = it.next().getPlayers(); 

- 在這裏>我得到的以下例外情況如下:

java.util.NoSuchElementException 
    at java.util.HashMap$HashIterator.nextEntry(Unknown Source) 
    at java.util.HashMap$KeyIterator.next(Unknown Source) 
    at org.hibernate.collection.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistentCollection.java:577) 

我該如何獲得一組聯盟對象,其中每個球隊都有一組球員?基本上,我怎麼獲得雙左加入?

+0

你確定這個問題是不是在你迭代結果的方式?它看起來像'下一個()'被調用,而不使用'hasNext前面的勾()'。 – axtavt 2011-04-18 16:39:02

回答

0

看起來像你設置錯誤的參數。

Query query = em.createNamedQuery("League.findJasperLeagueDetail"); 
//query.setParameter("leagueId", league.getId()); <--no named parameter 'leagueId' 
query.setParameter("Id", league.getId()); 

喝彩!