When lazy enhancement is enabled and given this entity {code:java} @Entity public class CommonEntity extends Base {
private User user; (...) @ManyToOne(fetch = LAZY) public User getUser() { return user; }
public CommonEntity setUser(User user) { this.user = user; return this; } } {code}
When this entity is queried: {code:java} s.createCriteria(CommonEntity.class).list() {code}
The related user is always retrieved from DB: {noformat} Hibernate: select this_.id as id1_2_0_, this_.name as name1_3_0_, this_.user_id as user_id3_3_0_ from CommonEntity this_ inner join Base this_1_ on this_.id=this_1_.id Hibernate: select user0_.id as id1_2_0_, user0_.name as name1_6_0_, jafsid1_.id as id1_2_1_, jafsid1_.sid as sid1_4_1_, groups2_.members_id as members_2_9_2_, usergroup3_.id as groups_i1_9_2_, usergroup3_.id as id1_2_3_ from User user0_ inner join SidEntity user0_1_ on user0_.id=user0_1_.id inner join Base user0_2_ on user0_.id=user0_2_.id left outer join JafSid jafsid1_ on user0_.id=jafsid1_.relatedEntity_id left outer join Base jafsid1_1_ on jafsid1_.id=jafsid1_1_.id left outer join UserGroup_JafSid groups2_ on jafsid1_.id=groups2_.members_id left outer join UserGroup usergroup3_ on groups2_.groups_id=usergroup3_.id left outer join SidEntity usergroup3_1_ on usergroup3_.id=usergroup3_1_.id left outer join Base usergroup3_2_ on usergroup3_.id=usergroup3_2_.id where user0_.id=? {noformat}
This is due to the shortcut in DefaultLoadEventListener#proxyOrLoad: {code:java} if ( !persister.hasProxy() ) { return load( event, persister, keyToLoad, options ); } {code} where hasProxy() is implemeted in AbstractEntityPersister: {code:java ] } public boolean hasProxy() { return this.entityMetamodel.isLazy() && !this.entityMetamodel.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading(); } {code}
So this is always "false" for lazy enhanced entity. I don't think that always issuing db queries in a relation is a desired effect :)
Test case: https://github.com/nikowitt/hibernate-test-case-templates/tree/HHH-12642-alwayloadfromdb |
|