Lazy enhanced entity as relationship is always loaded in a criteria query

Description

When lazy enhancement is enabled and given this entity

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @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; } }

When this entity is queried:

1 s.createCriteria(CommonEntity.class).list()

The related user is always retrieved from DB:

1 2 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=?

This is due to the shortcut in
DefaultLoadEventListener#proxyOrLoad:

1 2 3 4 // this class has no proxies (so do a shortcut) if ( !persister.hasProxy() ) { return load( event, persister, keyToLoad, options ); }

where hasProxy() is implemented in AbstractEntityPersister:

1 2 3 public boolean hasProxy() { return this.entityMetamodel.isLazy() && !this.entityMetamodel.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading(); }

So this is always "false" for a lazy enhanced entity. I don't think that always issuing db queries while loading a relation is a desired effect

Test case: https://github.com/nikowitt/hibernate-test-case-templates/tree/HHH-12642-alwayloadfromdb

Environment

None

Status

Assignee

Unassigned

Reporter

Niko Wittenbeck

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.3.1

Priority

Major
Configure