简体   繁体   English

默认情况下,GORM一对多映射会进行快速获取

[英]GORM one-to-many mapping doing eager fetching by default

Using GORM/Grails 2.3.7/Hibernate 3.6, NO scaffolding. 使用GORM / Grails 2.3.7 / Hibernate 3.6,无脚手架。

It's my understanding Hibernate uses lazy loading, so why does my mapping eagerly fetch? 我的理解是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' 
   }

Problem 问题

When calling Team.get(id) , the returned object contains all mailboxes, fully instantiated. 调用Team.get(id) ,返回的对象包含完全实例化的所有邮箱。 I was expecting mailboxes to be null unless invoking team.getMailboxes() , is the mapping wrong? 我期望邮箱为空,除非调用team.getMailboxes() ,映射是否错误?

Tried 试过了

Removed back-reference in Mailbox: 在邮箱中删除了反向引用:

 class Mailbox {
      Long id
      //Team team
      static belongsTo = Team   //before [team:Team]

Had no impact, 没有影响,

Also confirmed from sql output Hibernate is eargerly fetching the mailboxes: 还从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=?

I thought maybe it was related to hibernate cache, but I've disabled it: 我以为可能与休眠缓存有关,但是我禁用了它:

hibernate {
    cache.use_second_level_cache = false
    cache.use_query_cache = false
    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
    singleSession = false
}

Your understanding of lazy loading is wrong. 您对延迟加载的理解是错误的。 If you define an object or collection to loaded lazily, it does NOT mean, that they should return a null . 如果将对象或集合定义为延迟加载,则并不意味着它们应返回null Instead, you get a proxy a collection of proxies back, which get "inflated" as soon as you explicitly access the properties of the lazy-loaded object 取而代之的是,您获得一个proxy collection of proxies返回collection of proxies ,一旦您显式访问延迟加载的对象的属性,这些collection of proxies就会“膨胀”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM