When lazy enhancement is enabled and given this entity
@Entity
public class CommonEntity extends Base {
private String name;
private User user;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne(fetch = LAZY)
public User getUser() {
return user;
}
public CommonEntity setUser(User user) {
this.user = user;
return this;
}
}
When this entity is queried:
s.createCriteria(CommonEntity.class).list()
The related user is always retrieved from DB:
This is due to the shortcut in DefaultLoadEventListener#proxyOrLoad:
if ( !persister.hasProxy() ) {
return load( event, persister, keyToLoad, options );
}
where hasProxy is implemeted in AbstractEntityPersister: {code:java] public boolean hasProxy() { return this.entityMetamodel.isLazy() && !this.entityMetamodel.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading(); }
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 |