Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3788500
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:00:33+00:00 2026-05-19T12:00:33+00:00

I know hibernate has lazy as default fetching strategy, but there are some things

  • 0

I know hibernate has lazy as default fetching strategy, but there are some things unclear to me so i hope you can explain it to me. What i want to do is to get a tile marked as start tile.

Query:

@NamedQuery(name = "Tile.findStartTileByGame", 
  query = "SELECT t FROM Tile t WHERE t.game = :game " +
    "and t.startTile = true and t.blockWalkable = false")

Tile:

 public class Tile{ 

 @OneToOne(mappedBy="tile")
 private GameCharacter character;

 @OneToOne(mappedBy="tile")
 private GameObject gameObject;

Game:

 @OneToMany(mappedBy="game")
 private List<Tile> tiles;

When i run my query and never use the object hibernate still joins me character and gameobject. So i have 3 Queries. I know i can solve this by a fetch join, but my question is why does hibernate fetch both entites at all? Even when i annotate them with fetch=FetchType.LAZY it will be queried.

My DAO:

  public static Tile getFreeStartTile(EntityManager em, Game game) {
   TypedQuery<Tile> query = em.createNamedQuery("Tile.findStartTileByGame", Tile.class);
   query.setParameter("game", game);
  List<Tile> result = query.getResultList();
  ...

Before i fix this i would like to understand why it happens.
Thanks in advance
m

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:00:34+00:00Added an answer on May 19, 2026 at 12:00 pm

    The reason it happens is that they’re marked nullable. When proxying is used, Hibernate still needs to know the difference between ones to proxy and ones that are actually just plain null.

    When the relationship is a collection, it can give you an empty collection then check later. But when it’s OneToOne, it has to look in the DB anyway to find out if the field should be null, or if it could be proxied and lazy loaded. Since it has to look anyway, it just loads it.

    If you mark both fetch=FetchType.LAZY and optional=false you can get lazy fetching on onetoone. Of course if the column actual is nullable, you’re SOL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using hibernate to insert some rows but I don't know if I'm doing
I know that the session is used for the database in Hibernate, but what
I know there are non-standard frameworks such as commons-validator, and hibernate validator. I wanted
An object I mapped with hibernate has strange behavior. In order to know why
I know this has been asked and answered a number of times, but I
I don't know why but hibernate tries to cast BigDecimal to one of my
I know hibernate is an implementation of JPA, and also has its own features.
I know this question has been asked before but I have a design question
I need to know how can I set Hibernate (...) to achieve the following
Does anyone know how I can do the equivalent of this in hibernate: session.getIdentifier(instance);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.