Java – JPA 2.0 / Hibernate: why does lazy use @ onetoone out of the box?

My question is about the relationship between JPA 2.0 and hibernate, @ onetoone and delayed loading

First, my settings:

> Spring 3.0. 5.RELEASE > SprnigData JPA 1.0. 1.RELEASE > Hibernate 3.5. 2 - Final > DBMS: PostgreSQL 9.0

I recently found that the @ onetoone relationship cannot be obtained in a lazy way (fetchtype. Lazy), at least without byte code detection, compile time weaving, etc Many websites 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, under my settings, the delayed loading of @ onetoone entity seems to be "out of the box". I really want to know why Take a look at my unit tests:

@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. In the hibernates SQL log output, I can clearly see that "avatarimage" will not be extracted, but "user" (there is only one select, no join, no access to the "avatarimage" table), etc.)

The one-way @ onetoone relationship service in the user class is as follows:

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

So everything is simple - it seems to work

Repeat my question: why can it work and why can it lazily take out "avatarimage", although it is referenced by @ onetoone association?

I am very grateful for any help you have provided

Thank you.

Solution

The only problem with delaying the loading of the onetoone relationship is its reverse part (the one marked with the mappedby attribute) It works well in relation to ownership Ť The difference between them is clear at the database level In your case, the question is whether the user database table takes the ID of avatarimage as one of its columns or vice versa If the user table has a column with an ID of avatarimage, deferred loading will work as you say "out of the box", but it won't work the other way around

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>