使用grails,HQL的复杂SQL查询

时间:2014-04-23 08:48:11

标签: grails hql gorm

我正在尝试用HQL grails编写一个3表连接查询,我的3个类是:

class Domains {
    String name
    Date ets
    Account account

    static hasMany = [facebooks: Facebook, twitters: Twitter]
}

...

class Facebook {
    Boolean activated
    String name
    String token
    String pageId
    String expiryTime
    String scope
    Date ets
    String username
    Domains domains

    static belongsTo = [Domains]
}

...

class FbPosts {
    String postId
    Long commentsCount
    Long likesCount
    Long sharesCount
    Date date
    Date ets
    String message
    String type
    Integer postImpression
    Facebook facebook

    static belongsTo = [Facebook]
}

我正在尝试将fbPosts与域名(域名eid)相关联。我当前的HQL查询如下所示:

def fbPosts = FbPosts.findAll("from FbPosts as fb join Facebook as f on f.eid=fb.facebook_eid join Domains as d on d.eid=f.domain_eid where d.eid=?"[domain_eid])
def map = [fbPosts:fbPosts]

并且似乎不起作用。请帮忙。

2 个答案:

答案 0 :(得分:0)

是否适用于Criteria?

def fbPosts = FbPosts.withCriteria {
    facebook {
        domains {
            eq( 'eid', domain_eid )
        }
    }
}

答案 1 :(得分:0)

那怎么样?缺点是它将导致两个单独的数据库查询(除非域对象已经存在于持久性缓存中)。

def domainsInstance=Domains.get(domain_eid)
def fbPosts = FbPosts.withCriteria {
    facebook {
        eq('domains', domainsInstance)
    }
}