我知道有很多关于 join 与 join fetch 的问题。区别解释如下:
“获取”连接允许使用单个选择来初始化值的关联或集合以及它们的父对象。这在集合的情况下特别有用。它有效地覆盖了关联和集合的映射文件的外部连接和惰性声明。
但是,当我有一个使用内部或外部联接而不进行获取的标准或 HQL 时,我注意到实体似乎已完全获取(我的休眠映射使用 FetchType.LAZY)。我只在写“join fetch”时才预料到这一点。所以我看不出这种情况有什么不同。
我一定在这里遗漏了一些东西。有人可以帮我澄清一下吗?
更新
StringBuilder hql = new StringBuilder();
hql.append("select question ")
.append("from User as user ")
.append("left join user.definition as definition ")
.append("left join definition.sections as section ")
.append("left join section.questions as question ")
.append("where user.id = :user");
Query query = getQuery(hql.toString());
query.setParameter("user.id", userId);
return query.list();
当我检查问题列表的第一项时,我得到以下信息:
questions = {java.util.ArrayList@6756} size = 18
[0]={model.Question@6800}"model.Question@10f98160[]"
section = {Section$$_javassist_21@6873}"model.Section@7711057b[]"
handler = {org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer@6890}
interfaces = {java.lang.Class[1]@6897}
constructed = true
persistentClass = {java.lang.Class@2708}"class model.Section"
getIdentifierMethod = null
setIdentifierMethod = null
overridesEquals = true
componentIdType = null
replacement = null
entityName = {java.lang.String@6898}"model.Section"
id = {java.lang.Long@6881}"1"
target = {model.Section@6899}"model.Section@7711057b[]"
name = {java.lang.String@6909}"General"
sequence = 1
textStart = {java.lang.String@6910}"Some value"
textEnd = null
sectionVersion = 1
status = {model.SectionStatus@6911}"ACTIVE"
definitions = {org.hibernate.collection.internal.PersistentSet@6912} size = 1
questions = {org.hibernate.collection.internal.PersistentSet@6913} size = 13
validFrom = {java.sql.Timestamp@6914}"2012-01-01 00:00:00.0"
validTo = {java.sql.Timestamp@6915}"2099-01-01 00:00:00.0"
active = false
uuid = {java.util.UUID@6916}"97277496-12ee-453d-9478-9fc4e1519c02"
id = {java.lang.Long@6881}"1"
version = {java.lang.Integer@6882}"1"
initialized = true
readOnly = false
unwrap = false
session = {org.hibernate.internal.SessionImpl@6900}"SessionImpl(PersistenceContext[entityKeys= [EntityKey[model.PossibleAnswer#57], EntityKey[model.PossibleAnswer#56], EntityKey[PossibleAnswer#59], E...readOnlyBeforeAttachedToSession = null sessionFactoryUuid = null specjLazyLoad = false
Question 类引用了它的 Section,在堆栈跟踪中您可以看到 Section 已完全加载,尽管我没有在 HQL 中指定“join fetch”。