简体   繁体   English

Hibernate-简单查询懒惰获取

[英]Hibernate - simple query lazy fetching

I'm using the Play Framework with Hibernate and JPA. 我正在将Play框架与Hibernate和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 : 我想检索所有玩家,所以这是我使用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. 结果已经存储在一个列表中: List<Player> ,当我想调试一下列表中的内容时,我可以看到104个播放器,但是列表中只有5个第一播放器被真正加载。 Other players are lazy fetched : the class name is something like Player_$$_javassist_22 . 其他玩家则懒惰地获取:类名类似于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 谢谢

In a XML hibernate mapping you would simply write: <class name="Player" lazy="false">... which causes your player objects to be fetched eagerly. 在XML休眠映射中,您只需编写: <class name="Player" lazy="false">... ,这将导致急切地获取播放器对象。

With annotations you can do the same using @Proxy(lazy=false) as described here 使用注释您可以使用做同样的@Proxy(lazy=false)所描述的在这里

further information you can find here 您可以在此处找到更多信息

您可以在关联上使用@ManyToOne(fetch = FetchType.EAGER)以禁用延迟加载。

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 首先让我们了解'javax.persistence.FetchType'的概念,假设您有两个实体,例如“ Player”“ Team” ,它们之间具有@oneToMany关系

@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. 现在,当您从数据库加载团队时 ,JPA会为您加载其名称字段。 But you have two options for players : to load it together with the rest of the fields (ie eagerly[ FetchType.EAGER ]) or to load it on-demand (ie lazily[ FetchType.LAZY ]) when you call the Teams getPlayers() method. 但是,玩家有两个选择:将其与其余字段一起加载(即FetchType.EAGER [ FetchType.EAGER ]),或在调用Teams getPlayers()时按需加载(即lazily [ FetchType.LAZY ])。 ) 方法。

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. 因此由您决定哪种FetchType适合您的问题。 If you really want to load all players while loading the team then use FetchType.EAGER . 如果您真的想在加载团队时加载所有玩家,请使用FetchType.EAGER

hope this will solve your problem.... 希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM