JPA 2.0 / Hibernate:为什么LAZY使用“@ OneToOne”获取数据开箱即用? [英] JPA 2.0 / Hibernate: Why does LAZY fetching with "@OneToOne" work out of the box?
问题描述
我的问题是有关Hibernate的JPA 2.0,@OneToOne关系和延迟加载。
首先我的设置:
- Spring 3.0.5.RELEASE
- SprnigData JPA 1.0.1.RELEASE
- Hibernate 3.5.2-最后
- DBMS:PostgreSQL 9.0我最近遇到这样一个事实,即@OneToOne关系不能以懒惰的方式获取(FetchType.LAZY),至少不能没有字节码检测,编译时间编织等等。例如,许多网站都会这样说: http://community.jboss.org/wiki/SomeExplanationsOnLazyLoadingone-to-one
- http://justonjava.blogspot.com/2010/ 09 / lazy-one-to-one-and-one-to-many.html
- 让一对一关系懒惰
我的设置,@OneToOne实体的延迟加载似乎开箱即用,我真的很想理解为什么。请看看我的单元测试:
@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));
}
这个测试用例很成功,在Hibernates SQL日志记录输出中,我可以看到显然,不会获取avatarImage,只是用户(只是一个SELECT,不加入,不能访问AvatarImage表等)。
<
@OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private AvatarImage avatarImage;
所以,一切都很简单 - 看起来很有效。
重复我的问题:为什么它工作,为什么可以懒惰地提取AvatarImage,尽管它是通过@OneToOne关联引用的?
我非常感谢您提供的任何帮助
非常感谢!
OneToOne关系的延迟加载问题只在它的反向部分(用mappedBy属性标记的那部分)上。它在关系的拥有方面运作良好。 T
之间的区别在数据库级别是明确的。在你的情况下,问题是如果用户数据库表中包含一个AvatarImage id作为其中一列或其他方式。
如果用户表有一个ID为AvatarImage的列,那么延迟加载就像你说的开箱即用一样工作,但它不会反过来工作。
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!
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.
这篇关于JPA 2.0 / Hibernate:为什么LAZY使用“@ OneToOne”获取数据开箱即用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!