简体   繁体   English

休眠如何与延迟获取引用一起工作?

[英]How hibernate works with lazy fetching references?

First I will fetch a list of Game objects form database using hibernate 首先,我将使用休眠模式获取游戏对象列表数据库的列表

List<Game> games = (List<Game>) session.createQuery("from Game where status=2").list();

Consider that Game Object has a reference to Location object. 考虑游戏对象具有对位置对象的引用。 And Location object has an Id. 位置对象有一个ID。

long id = games.get(0).getLocation().getId();

Here I need to know the Id of the location where game is mapped to. 在这里,我需要知道游戏映射到的位置的ID。 And Location is defined as a lazy fetch. 位置定义为懒惰获取。 But in the database each row for game has the location_id field. 但是在数据库中,游戏的每一行都有location_id字段。

So my question is when I request hibernate to fetch the Id through an object as shown, is it first fetching the object and then the Id of that? 所以我的问题是,当我请求休眠状态通过一个对象获取ID时,如图所示,它是先获取对象,然后获取该对象的ID吗? or is it more intelligent and fetch the Id directly from the game? 还是更智能并直接从游戏中获取ID?

It will produce 2 statements. 它将产生2条语句。

First the Select for the Game including the Gameid and the locationid 首先选择游戏,包括Gameid和locationid

Second the Select for the Whole location-row including the LocationId. 其次,选择整个位置行(包括LocationId)。

Why? 为什么? Because after getLocation() the Runtime will not know that you only need the id . 因为在getLocation() ,运行时将不知道您只需要id

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

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