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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:52:40+00:00 2026-05-16T22:52:40+00:00

I have Hibernate Entities that look something like this (getters and setters left out):

  • 0

I have Hibernate Entities that look something like this (getters and setters left out):

@Entity
public class EntityA {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    private EntityB parent;
}

@Entity
public class EntityB extends SuperEntity {
    @OneToMany(mappedBy = "parent")
    @Fetch(FetchMode.SUBSELECT)
    @JoinColumn(name = "parent_id")
    private Set<EntityA> children;
}

@MappedSuperclass
public class SuperEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long itemId;
}

When I query for EntityA it loads fine, with the parent association being replaced by a Hibernate proxy (as it is Lazy). If I want access to the parent’s id I perform the following call:

EntityA entityA = queryForEntityA();
long parentId = entityA.getParent().getItemId();

As I understand that call should NOT make a roundtrip to the database, as the Id is stored in the EntityA table, and the proxy should only return that value. However, in my case this generates a SQL statement which fetches EntityB and only then returns the Id.

How can I investigate the problem? What are some likely causes of this incorrect behaviour?

  • 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-16T22:52:41+00:00Added an answer on May 16, 2026 at 10:52 pm

    As I understand that call should NOT make a roundtrip to the database, as the Id is stored in the EntityA table, and the proxy should only return that value.

    Use property access type. The behavior you’re experiencing is a “limitation” of field access type. Here is how Emmanuel Bernard explained it:

    That is unfortunate but expected. That’s one of the limitations of field level access.
    Basically we have no way to know that getId() indeed only go and access the id field. So we need to load the entire object to be safe.

    So change your code into:

    @Entity
    public class EntityA {
        private EntityB parent;
    
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "parent_id")
        public EntityB getParent() {
            return parent; 
        }
        ...
    }
    
    @MappedSuperclass
    public class SuperEntity {
        private long itemId;
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id")
        public long getItemId() { 
            return itemId;
        }
        ...
    }
    

    Related question

    • Hibernate Annotations – Which is better, field or property access?

    References

    • Proxy loaded on getId-call when using annotations on fields
    • proxy getId => why sql is generated !
    • HHH-3718 (if this issue can ever be solved)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a couple of entities that look like this: @Entity public
I have a Hibernate mapping that looks something like this: <class name=MyEntity> <set name=scalarSet
I have the following (simplified) Hibernate entities: @Entity @Table(name = package) public class Package
I have hibernate configured to print out the sql statements that it generates and
I'm trying to build a Hibernate Criteria query to find entities that have a
I have some Hibernate entities that I want to group by paramType and display
I have the following (simple) mapping: @Entity public class Role { @OneToMany( fetch =
I have an RMI class that accepts remote calls from clients. This class uses
This is the setup for my 2 entities: public class Person { public Guid
I have a page that has a list of Hibernate entities that is loaded

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.