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 8873639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:30:54+00:00 2026-06-14T18:30:54+00:00

I have the Problem that a quite simple select (SELECT h FROM Hero h

  • 0

I have the Problem that a quite simple select (SELECT h FROM Hero h where h.owner = :player) causes a n+1 Problem. The query triggers the select to the game for each hero entity.

First i added fetch type lazy which removed the alternative queries during the select. In my code i need the game only once to get the id. This triggered the select again for every entity.

So i added a join fetch which helped, BUT it causes a join i don’t need! How can i tell the hibernate proxy object to give me the id without fetching the whole entity?

I tried the tips there without success: http://256stuff.com/gray/docs/misc/hibernate_lazy_field_access_annotations.shtml

Both @AccessType from hibernate and @Access from javax make no difference. The game will always be fetched when i call getId().

Any Ideas what i am missing? I would need this quite often and i would like to avoid joins if i can.

greetings,
markus

PS: I use jpa2 + hibernate 4 with jboss 7.

@Entity
@NamedQueries({ 
    @NamedQuery(name = "Hero.findByPlayer", query = "SELECT h FROM Hero h JOIN FETCH h.game where h.owner = :player") 
    })
public class Hero extends GameCharacter implements Serializable {   
    @ManyToOne
    private Player owner;

@Entity
public abstract class GameCharacter extends GameObject implements Serializable {
     ... nothing special in here
}

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class GameObject implements Serializable {
    @Id
    private String id = java.util.UUID.randomUUID().toString();

    @ManyToOne(fetch=FetchType.LAZY)
    protected Game game;

@Entity
public class Game implements Serializable {
    private static final long serialVersionUID = 4379242677193301727L;

    @Id
    private String id = java.util.UUID.randomUUID().toString();
  • 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-06-14T18:30:55+00:00Added an answer on June 14, 2026 at 6:30 pm

    This is just a side affect of the way lazy loading in hibernate works. Essentially, any access of the lazy object will cause it to populate. It doesn’t make a special case of the id.

    I don’t think you’re actually going to be able to access the ID from the game object without triggering the loading. What you can do is ask hibernate for the id of an object using the getIdentifier method. I believe this will not trigger the fetch as hibernate is simply looking up the metadata it has for the object.

    Alternatively you can load the game id alongsite the hero when executing your query.

    E.g.

    SELECT h, h.game.id FROM Hero h where h.owner = :player
    

    Rather than finding you a list of heros as your current query does, this will return a list of pairs, each pair having the hero and the game id.

    ID is treated specially in HQL so this shouldn’t trigger the fetch or join you’re getting when trying to access the object.

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

Sidebar

Related Questions

I have a problem that is quite simple to understand, but for me it
I have a performance issue regarding a quite simple query that run for more
I have quite simple problem in SQL Server: User can have 12 items that
I have a problem that I'm working on for quite some time now. I
I have a strange problem that I can't seem to solve. I've quite a
I have a problem with Javascript that keeps bugging me for quite a while
I have this quite popular problem, but have failed to find a solution that
I have a problem with a relatively simple query and the execution plan Access
I have a query that I'm executing from a .NET application to a SQL
In a couple of scripts that I use I have problem that is intermittent.

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.