Note that there are some explanatory texts on larger screens.

plurals
  1. POhibernate join fetch does not fetch all children
    text
    copied!<p>I'm using the play framework and JPA and Hibernate.</p> <p>I have several entities :</p> <pre><code>@Entity public class Player extends Model { public enum Gender { MALE, FEMALE } @Required public String name; @Required public Gender gender; @Required public Long gold; } @Entity public class Building extends Model { @Required @ManyToOne(fetch = FetchType.LAZY) public Player owner; @Required public String buildingType; @Required public Long buildingId; } @Entity public class Stock extends Model { // Either a product type or a raw material @Required public Long goodId; @Required public Boolean isProduct; @Required public String image; } @Entity public class Contract extends Model { @Required @ManyToOne(fetch = FetchType.LAZY) public Player player; @Required @ManyToOne(fetch = FetchType.LAZY) public Building building; @Required @ManyToOne(fetch = FetchType.LAZY) public Stock stock; @Required public Long ordersLeft; @Required public Long cyclesLeft; } </code></pre> <p>With these entities I would like to retrieve all my <code>Contract</code> and their associated <code>Building</code>, <code>Stock</code> and <code>Player</code></p> <p>Here is the query I' using :</p> <pre><code>public static List&lt;Contract&gt; retrieveContractsForNewOrder() { return find("select distinct c from Contract c " + "left join fetch c.player " + "left join fetch c.stock " + "left join fetch c.building " + "where c.cyclesLeft = 0 and c.ordersLeft &gt; 0").fetch(); } </code></pre> <p>The query is working, I retrieve my list of <code>Contract</code>. However, the <code>building</code> and <code>stock</code> variables are loaded but the <code>player</code> variable is not (The class name in the <code>Contract</code> object is <code>Player_$$_javassist_22</code> and it has a handler named <code>JavassistLazyInitializer</code>).</p> <p>I don't know what I'm doing wrong and why Hibernate refuses to fetch the player model while it's fetching the other models ...</p> <p>Thank you for your help</p> <p><strong>EDIT</strong></p> <p>After some tests the query executed by hibernate seems correct : all joins are there and all fields from all models are in the select.</p> <p>However the results are strange : I'm only retrieving fields from <code>Contract</code> and <code>Stock</code> (not from <code>Building</code> and <code>Player</code>).</p> <p>But my <code>Building</code> is loaded, why ? Because of the cache ? And why <code>Building</code> and <code>Player</code> does not appear in results ?</p> <p><strong>EDIT 2</strong></p> <p>I tried to execute the exact same request directly in MySQL and I'm retrieve all the variables I need, while Hibernate seems to skip some variables (they are not all in the results).</p> <p>What's happening ?</p> <p><strong>EDIT 3</strong></p> <p>I tried to <code>clear()</code> the session cache before doing my query and guess what ? The <code>Player</code> is loaded ...</p> <p>I tried to only load the <code>Player</code> using the <code>left join fetch</code> but it was still not working (but <code>Building</code> was loaded thanks to the cache). <code>Player's</code> fields were not in the results. However, as soon as I cleared the cache the <code>Player's</code> fields appeared in the results and the <code>Player</code> is loaded in my <code>Contract</code>.</p> <p>Why clearing the cache solved the issue? <code>Player</code> was in the cache ? If so, why wasn't it loaded? I don't understand what happens, but I can't just clear the cache as I need to make a lot of other queries after this one and I need the cache for them.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload