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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:20:25+00:00 2026-06-11T08:20:25+00:00

my question is regarding JPA 2.0 with Hibernate, @OneToOne relationships and lazy loading. First

  • 0

my question is regarding JPA 2.0 with Hibernate, @OneToOne relationships and lazy loading.

First my setup:

  • Spring 3.0.5.RELEASE
  • SprnigData JPA 1.0.1.RELEASE
  • Hibernate 3.5.2-Final
  • DBMS: PostgreSQL 9.0

I recently came across the fact, that a @OneToOne relationship can’t be fetched the lazy way (FetchType.LAZY), at least not without byte code instrumentation, compile time weaving or the like. Many sites out there say this, for example:

  • http://community.jboss.org/wiki/SomeExplanationsOnLazyLoadingone-to-one
  • http://justonjava.blogspot.com/2010/09/lazy-one-to-one-and-one-to-many.html
  • Making a OneToOne-relation lazy

The thing is, with my setup, a lazy loading of a @OneToOne entity seems to work “out of the box”, and I really would like to understand why. Please, have a look at my unit test:

@Test
@Transactional
public void testAvatarImageLazyFetching()
{
    User user = new User();
    user.setAvatarImage( new AvatarImage() );

    User = userRepository.save( user );

    entityManager.flush();
    entityManager.clear();

    User loadedUser = userRepository.findOne( user.getId() );
    assertNotNull( loadedUser );

    PersistenceUtil persistenceUtil = Persistence.getPersistenceUtil();

    assertTrue( persistenceUtil.isLoaded( loadedUser ) );
    assertFalse( persistenceUtil.isLoaded( loadedUser, "avatarImage" ) );
}

This test case is successful, and in Hibernates SQL logging output, I can see clearly, that the “avatarImage” will not be fetched, just the “user” (just a single SELECT, no JOIN, no access to the “AvatarImage” table etc.)

The unidirectional @OneToOne relationshop in the User class looks like this:

@OneToOne( cascade = CascadeType.ALL, fetch = FetchType.LAZY )
private AvatarImage    avatarImage;

So, everything very simple – and it seems to work.

To repeat my question: why is it working, why can the “AvatarImage” be fetched lazily, although it is referenced with a @OneToOne association?

I really appreciate any help you can offer

Thanks a lot!

  • 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-11T08:20:27+00:00Added an answer on June 11, 2026 at 8:20 am

    The problem with lazy loading of OneToOne relationship is only on the inverse part of it (the one which is marked with mappedBy attribute). It works fine on the owning side of the relationship. T
    he difference between those is clear on database level. In your case the question is if the User database table holds an id of AvatarImage as one of the columns or the other way round.
    If User table has a column with an id of AvatarImage then the lazy loading will work as you said “out-of-box” but it will not work the other way round.

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

Sidebar

Related Questions

Question regarding how to setup dbase relationships (newbie, this may be trivial) Followed the
Following my previous question, DAO and Service layers (JPA/Hibernate + Spring) , I decided
I have a question regarding JPA persistence in Glassfish. Situation: I have a Supplier
I have a simple question regarding Entity declaration in JPA. I have an entity
Question regarding mountable engines on rails. First of all these are the versions I
I'm working with JPA(Hibernate) and spring project these days, I configured spring, jpa and
I have a question regarding the JPA OR mapping between two persistent entities with
This week I've been trying to learn Spring, JBoss, Maven, JPA and Hibernate and
Question regarding GtkBuilder. When we unref builder pointer does it destroys all the screens/widgets
Question regarding fluent nHibernate table mappings: Apparently I was under the mistaken impression, that

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.