使用GORM / Grails 2.3.7 / Hibernate 3.6,无脚手架。
我的理解Hibernate使用延迟加载,为什么我的映射急切地获取?
TEAM ---has many--> MAILBOX
class Team {
Long id
Long version
static hasMany = [mailboxes:Mailbox]
static mapping = {
id sqlType:'number', generator: 'native'
version sqlType:'number'
}
class Mailbox {
Long id
Team team
static belongsTo = [team:Team]
static mapping = {
id sqlType:'number', generator: 'native'
}
调用Team.get(id)
时,返回的对象包含完全实例化的所有邮箱。除非调用team.getMailboxes()
,否则我期望邮箱为空,映射是否错误?
删除了邮箱中的反向引用:
class Mailbox {
Long id
//Team team
static belongsTo = Team //before [team:Team]
没有影响,
还从sql输出确认Hibernate正在抢夺邮箱:
Hibernate: select team0_.id as id1_0_, team0_.version as version1_0_, team0_.name as name1_0_ from team team0_ where team0_.id=?
Hibernate: select mailboxes0_.team_mailboxes_id as team1_1_0_, mailboxes0_.mailbox_id as mailbox2_0_ from team_mailbox mailboxes0_ where mailboxes0_.team_mailboxes_id=?
Hibernate: select mailbox0_.id as id0_0_, mailbox0_.version as version0_0_, mailbox0_.name as name0_0_ from mailbox mailbox0_ where mailbox0_.id=?
我想也许这与hibernate缓存有关,但我已经禁用了它:
hibernate {
cache.use_second_level_cache = false
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
singleSession = false
}
答案 0 :(得分:0)
您对延迟加载的理解是错误的。如果你定义一个懒惰加载的对象或集合,那并不意味着它们应该返回null
。相反,你会得到一个proxy
一个collection of proxies
的回复,这会导致"膨胀"只要您显式访问延迟加载对象的属性