Why does Hibernate execute multiple SELECT queries instead of one when using @Fetch(FetchMode.JOIN)

后端 未结 2 1946
我寻月下人不归
我寻月下人不归 2021-01-31 04:41

I\'ve got the following query which I expect to run in a single select request:

@NamedQuery(name=Game.GET_GAME_BY_ID1,
                query = \"SELECT g FROM Ga         


        
2条回答
  •  鱼传尺愫
    2021-01-31 05:37

    The secondary queries come from:

    @ManyToOne(fetch=FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    @JoinColumn(name="team_id2")
    public Team getTeam2() {
        return team2;
    }
    

    So, you need to:

    1. Make all associations LAZY. By default, all @ManyToOne and @OneToOne associations are EAGER, so it's better to have them LAZY and only override the fetch plan on a query basis.

    2. Remove the @Fetch(FetchMode.JOIN), which is essentially an EAGER fetch directive. In your case, not just the team2 property is fetched, but its players and skills as well.

提交回复
热议问题