java - Why does this result in non-lazy fetching and N+1 select statements? -
I just hope to have a SQL query, but I'll go to the N + 1 selected network. I really do not understand why there is a problem in detail here:
I have a unit "player":
@inti @Table (name = " Player_ref ") Public class PlayerRef {// constructor etc ... @OptimasticalLock (removed = true) @OneToMany (fetch = FetchType.LAZY, mapped =" player ") public set & lt; Player & gt; Millplayer () {Return Player; }}
and a class player:
@Entity @Table (name = "player") public class player {// constructor etc ... @OptimisticLock (excluded = true) @ManyToOne (optional = true, bringing = FetchType.LAZY) @JoinTable (name = "cr_player_ref_player", joinColumns = {@JoinColumn (name = "player_id", unique = true)}, inverseJoinColumns = {@JoinColumn} Public player Reef Miller () {This return player.}}
Now, in my program I use the following HQL query to all playerRef entities To get:
query playerRefQ = session.createQuery ( "playerRef choose Pleyrrf Kiladhirf from") .setReadOnly (true); PlayerRefQ.setParameter ( "game", game); @SuppressWarnings ( "unchecked") List & lt; PlayerRef & gt; AllPlayerRefs = PlayerRefQ.list ();
It selects statements in N + 1:
1)
select playerref0_ as .id id1_21_, playerref0_ .. .. player_ref playerref0_
and
n time)
players0_.player_ref_id player_r1_21_0_, player_i2_34_0_ as players0_.player_id, Select as id1_19_1_finders1_.id, player1_. ..., as player1_1_.player_ref_id player_r1_34_1_, ... from cr_player_ref_player on players0_ the inner join players0_.player_id = player1_.id player player1_ external player1_.id = player1_1_.player_id where joining players0_.player_ref_id = cr_player_ref_player player1_1_ Left for?
Since I thought the collection should be a hibernate-proxy lazy load and each player has a set of players, it's very unpredictable.
Does anyone know how can I actually load players' reef units without loading them? In the case of my use I need all the players, but not related players.
The problem came from here, that your underlying DB structure is actually several-to- Many
is a pair table cr_player_ref_player
, and that means, that
- a
player
can be multiple PlayerRef < / Li> - You can see many
players
in a instead: < / P> should be there: player
Public player Reef Millpler () {This return player. }
public set & lt; PlayerRef & gt; GetPlayerRefs () {Returns player; } Confusion for
Hibernate
comes from: @OneToMany (fetch = FetchType.LAZY, mappedBy = "playerRef")
, Primarily that mapping will be found under the "playerRef"
... ... where the required is more complex and partially than expected Is expressed
many-to-many
. And that's why we can see these weird N + 1s ...
Comments
Post a Comment