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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:01:33+00:00 2026-05-20T17:01:33+00:00

I have a Hibernate entity Parent that is joined to another: Child . In

  • 0

I have a Hibernate entity Parent that is joined to another: Child. In the database I have a column parent.child_id that has a foreign key constraint on child.id. Assuming I’m lazily loading, when I load an instance of Parent I expect to be able to access parent.getChild().getId() without incurring a lookup on the child table.

I expected the proxy returned by parent.getChild() to be able to fulfil a request to Child.getId() as it has already loaded the child’s id from parent.child_id column. However, I see an additional query on the child table.

Why is this extra query necessary, and how can I avoid it? In this instance I’m only interested in the foreign key ID, and do not wish to load the whole row.

Classes:

class Parent {
  @Id @Column(name = "id", unique = true, nullable = false)
  private Integer id;

  @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "child_id")
  private Child child;
}

class Child {
  @Id @Column(name = "id", unique = true, nullable = false)
  private Integer id;

  @Column(name = "name")
  private String name;
}

Tables:

CREATE TABLE child (
    id int PRIMARY KEY,
    name varchar(10)
);

CREATE TABLE parent (
    id int PRIMARY KEY,
    child_id int REFERENCES (child.id)
);
  • 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-20T17:01:34+00:00Added an answer on May 20, 2026 at 5:01 pm

    It’s caused by the fact that Child uses field access (since annotations are placed on fields), therefore Hibernate simply initializes the proxy when you call any of its methods.

    If you move annotations to properies, it would work as expected.

    Since JPA 2.0 (Hibernate 3.5) you can configure it in fine-grained way:

    @Access(AccessType.FIELD) // Default is field access
    class Child {
        private Integer id;
    
        @Column(name = "name")
        private String name;
    
        @Access(AccessType.PROPERTY) // Use property access for id
        @Id @Column(name = "id", unique = true, nullable = false)
        public Integer getId() { ... }
    
        ...   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a category entity that has a category as parent and a list
We have a DB table that is mapped into a hibernate entity. So far
We have a project that uses JPA/Hibernate on the server side, the mapped entity
I have a problem with a query using hibernate. The entity class EntityClass has
consider this scenario: I have loaded a Parent entity through hibernate Parent contains a
I have an entity 'Parent' which has a Set of enums private Set<MyEnum> myEnums
I have a parent/child database relationship between the table ACCOUNT and the table USER.
I have a parent-child relationship: @Entity @Table(name = user) public final class User {
I'm working with Hibernate and JPA. I have an entity called Customer that references
I am trying to create an Entity that has a Parent and children from

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.