简体   繁体   中英

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.

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

Here I need to know the Id of the location where game is mapped to. And Location is defined as a lazy fetch. But in the database each row for game has the location_id field.

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? or is it more intelligent and fetch the Id directly from the game?

It will produce 2 statements.

First the Select for the Game including the Gameid and the locationid

Second the Select for the Whole location-row including the LocationId.

Why? Because after getLocation() the Runtime will not know that you only need the id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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