1

I'm using the Play Framework with Hibernate and JPA.

I have a simple entity :

@Entity
public class Player extends Model {

    @Required
    public Long gold;
}

I would like to retrieve all my players, so here is the query I execute using Hibernate :

select p from Player p

The result has been stored in a list : List<Player> and when I want to debug to see what is in the list I can see 104 players, but only the 5 first players of the list are really loaded. Other players are lazy fetched : the class name is something like Player_$$_javassist_22.

My question is : why aren't all my players entirely loaded? Why some of them are lazy fetched? I would like to have all my players loaded without lazy fetching, how can I do?

Thank you

3 Answers 3

1

In a XML hibernate mapping you would simply write: <class name="Player" lazy="false">... which causes your player objects to be fetched eagerly.

With annotations you can do the same using @Proxy(lazy=false) as described here

further information you can find here

Sign up to request clarification or add additional context in comments.

5 Comments

It does not work. I have to specify an association in the fetchoverride but I have no association, just my Player entity
Please try @Proxy(lazy=false)
Thank you, this is making my entities to be loaded! But that means that if y entity becomes a child of another entity, the Player entity will always be loaded without proxy ? Si so, is there a way to tell hibernate to not use the proxy for this entity only for a query? (like calling a function before a query to disable proxy for this entity, and then calling another function to enable it again)
Hibernate allows to define the fetch mode per query when you use a criteria per association as described here. When you use HQL only please check this
I'm using HQL but left join fetch didn't work (probably because of the cache (i can't clear the cache because i need it)) and with criteria I can't find something related to the proxy. What I would like to do is exactly what @Proxy(lazy=false) does but by calling a function from the session or entitymanager or something else. Is it possible?
0

You can use @ManyToOne(fetch = FetchType.EAGER) on your associations to disable lazy loading.

3 Comments

But here I have no association, this is a simple entity for which I would like to retrieve the 104 results, but these results are lazily fetched, why?
Maybe the class which you are extending from i.e Model has some associations?
No, Model just has an id property
0

first lets understand the concept of 'javax.persistence.FetchType' suppose you have two entities as 'Player' and 'Team' as folows having @oneToMany relationship between them

@Entity
@Table(name = "player")
public class Player extends Model {
    public Long gold;
    String name;    
}

and

@Entity
@Table(name = "team")
public class Team {
    @Column (name = "name") 
    public String name;

    @OneToMany(fetch = FetchType.LAZY,mappedBy="players")
    public List<Player> players;    
}

Now when you load a Team from the database, JPA loads its name field for you. But you have two options for players : to load it together with the rest of the fields (i.e. eagerly[FetchType.EAGER]) or to load it on-demand (i.e. lazily[FetchType.LAZY]) when you call the Teams getPlayers() method.

When a Team has many players it is not efficient to load all of its players with it when they are not needed. So in suchlike cases, you can declare that you want players to be loaded when they are actually needed. This is called lazy loading.

so its up to you which FetchType is suitable for your problem. If you really want to load all players while loading the team then use FetchType.EAGER.

hope this will solve your problem....

3 Comments

Thank you, but in my case I don't have any relationship. I'm juste retrieving a list of Player with only one property (a long value) and some of the Player are lazily initialized (their gold property is not fetched)
@Fabien Henon The javax.persistence.FetchType is used when you have and relationship between two tables you can not deffine 'javax.persistence.FetchType' for single entity like @Column (name = "gold",fetch = FetchType.LAZY)
I know, that's why I don't understand why I'm not getting loaded players but only lazily fetched players. I think it comes from the cache, because when I clear the cache my players are loaded. However I can't clear it because I need it for other entities

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.