How to optimize this GORM query

寵の児 提交于 2019-12-04 15:12:11

You can use the load method to avoid the first database query:

def user = User.load(springSecurityService.principal.id)
def domainObject = Profile.findByUser(user)

This works because load doesn't trigger a database lookup until you access any property other than the id, but that's all that's needed for the findByUser since it's the foreign key.

I would do:

def profile = Profile.createCriteria().list {
    user{
        eq 'id',springSecurityService.principal.id
    }
}[0]

Or

def profile = Profile.executeQuery('select p from Profile p where p.user.id = :userId',[userId:springSecurityService.principal.id])[0]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!