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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:32:05+00:00 2026-05-16T07:32:05+00:00

Imagine 2 tables in a relational database, e.g. Person and Billing. There is a

  • 0

Imagine 2 tables in a relational database, e.g. Person and Billing. There is a (non-mandatory) OneToOne association defined between these entities, and they share the Person primary key (i.e. PERSON_ID is defined in both Person and Billing, and it is a foreign key in the latter).

When doing a select on Person via a named query such as:

from Person p where p.id = :id

Hibernate/JPA generates two select queries, one on the Person table and another on the Billing table.

The example above is very simple and would not cause any performance issues, given the query returns only one result. Now, imagine that Person has n OneToOne relationships (all non-mandatory) with other entities (all sharing the Person primary key).

Correct me if I’m wrong, but running a select query on Person, returning r rows, would result in (n+1)*r selects being generated by Hibernate, even if the associations are lazy.

Is there a workaround for this potential performance disaster (other than not using a shared primary key at all)? Thank you for all your ideas.

  • 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-16T07:32:06+00:00Added an answer on May 16, 2026 at 7:32 am

    Imagine 2 tables in a relational database, e.g. Person and Billing. There is a (non-mandatory) OneToOne association defined between these entities,

    Lazy fetching is conceptually not possible for non-mandatory OneToOne by default, Hibernate has to hit the database to know if the association is null or not. More details from this old wiki page:

    Some explanations on lazy loading (one-to-one)

    […]

    Now consider our class B has
    one-to-one association to C

    class B {
        private C cee;
    
        public C getCee() {
            return cee;
        }
    
        public void setCee(C cee) {
            this.cee = cee;
        }
    }
    
    class C {
        // Not important really
    }
    

    Right after loading B, you may call
    getCee() to obtain C. But look,
    getCee() is a method of YOUR class
    and Hibernate has no control over it.
    Hibernate does not know when someone
    is going to call getCee(). That
    means Hibernate must put an
    appropriate value into “cee”
    property at the moment it loads B from
    database. If proxy is enabled for
    C, Hibernate can put a C-proxy
    object which is not loaded yet, but
    will be loaded when someone uses it.
    This gives lazy loading for
    one-to-one.

    But now imagine your B object may or
    may not have associated C
    (constrained="false"). What should
    getCee() return when specific B
    does not have C? Null. But remember,
    Hibernate must set correct value of
    “cee” at the moment it set B
    (because it does no know when someone
    will call getCee()). Proxy does not
    help here because proxy itself in
    already non-null object.

    So the resume: if your B->C mapping
    is mandatory (constrained=true),
    Hibernate will use proxy for C
    resulting in lazy initialization. But
    if you allow B without C, Hibernate
    just HAS TO check presence of C at the
    moment it loads B. But a SELECT to
    check presence is just inefficient
    because the same SELECT may not just
    check presence, but load entire
    object. So lazy loading goes away
    .

    So, not possible… by default.

    Is there a workaround for this potential performance disaster (other than not using a shared primary key at all)? Thank you for all your ideas.

    The problem is not the shared primary key, with or without shared primary key, you’ll get it, the problem is the nullable OneToOne.

    First option: use bytecode instrumentation (see references to the documentation below) and no-proxy fetching:

    @OneToOne( fetch = FetchType.LAZY )
    @org.hibernate.annotations.LazyToOne(org.hibernate.annotations.LazyToOneOption.NO_PROXY)
    

    Second option: Use a fake ManyToOne(fetch=FetchType.LAZY). That’s probably the most simple solution (and to my knowledge, the recommended one). But I didn’t test this with a shared PK though.

    Third option: Eager load the Billing using a join fetch.

    Related question

    • Making a OneToOne-relation lazy

    References

    • Hibernate Reference Guide
      • 19.1.3. Single-ended association proxies
      • 19.1.7. Using lazy property fetching
    • Old Hibernate FAQ
      • How do I set up a 1-to-1 relationship as lazy?
    • Hibernate Wiki
      • Some explanations on lazy loading (one-to-one)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine we have two tables in the database, user (FK id_role) and role (PK
Imagine a database that tracks payments between customers. Say I've got a Customer table:
Imagine you have those 3 tables: And imagine there is massive data according to
imagine there are two tables. Order +----------------+ | ID | | Name | +----------------+
Imagine this scenario: I have three tables in my database: Products , Users and
So imagine you have multiple tables in your database each with it's own structure
Imagine the following tables: create table boxes( id int, name text, ...); create table
Imagine the following database: Table 'companies' has fields id, name and flagship_product_id. Table 'products'
Imagine I have these columns in a table: id int NOT NULL IDENTITY PRIMARY
I'm going crazy with this and would greatly appreciate some help. Imagine two tables

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.