I'm using the play framework and JPA and Hibernate.
I have several entities :
@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;
}
With these entities I would like to retrieve all my Contract and their associated Building, Stock and Player
Here is the query I' using :
public static List<Contract> 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 > 0").fetch();
}
The query is working, I retrieve my list of Contract. However, the building and stock variables are loaded but the player variable is not (The class name in the Contract object is Player_$$_javassist_22 and it has a handler named JavassistLazyInitializer).
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 ...
Thank you for your help
EDIT
After some tests the query executed by hibernate seems correct : all joins are there and all fields from all models are in the select.
However the results are strange : I'm only retrieving fields from Contract and Stock (not from Building and Player).
But my Building is loaded, why ? Because of the cache ?
And why Building and Player does not appear in results ?
EDIT 2
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).
What's happening ?
EDIT 3
I tried to clear() the session cache before doing my query and guess what ? The Player is loaded ...
I tried to only load the Player using the left join fetch but it was still not working (but Building was loaded thanks to the cache). Player's fields were not in the results.
However, as soon as I cleared the cache the Player's fields appeared in the results and the Player is loaded in my Contract.
Why clearing the cache solved the issue? Player 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.