在 Grails 中创建连接的 HQL 时失败

fail while creating HQL of join in Grails

我们要实现的目标-

subscribed topics of the user, ordered by latest post entered in the topic.

我做了什么-我在我的 grails 应用程序中使用以下 HQl 查询,它的 sql 在 mysql控制台-

List<Topic> subscriptionList=Topic.executeQuery("select T from Topic T
where id in(select topic from Subscription S join (select distinct(topic) DT 
from Resource order by last_updated desc) TR on  TR.DT=S.topic where user=user)");

这是抛出exception/error消息-

unexpected token: ( near line 1, column 101 [select T from com.ig.domain.Topic T where id in(select topic from com.ig.domain.Subscription S join (select distinct(topic) DT from com.ig.domain.Resource order by last_updated desc) TR on TR.DT=S.topic where user=user)].

以下是 IDE 显示的错误消息。 下面是型号 class/entities-

Resource.groovy

class Resource {

    String description
    Date lastUpdated
    Date dateCreated
    static belongsTo = [user:User,topic:Topic]
    static mapping = {
        user column:'createdBy'
        topic column: 'topic'
        version false
        description nullable:true, blank:true
     }
    static constraints = {}
}

Topic.groovy

    class Topic {

    String name
    Date dateCreated
    Date lastUpdated
    Visibility visibility
    static belongsTo = [createdBy:User]
    static hasMany = [subscription:Subscription,resource:Resource]

    enum Visibility {
        PUBLIC,PRIVATE
    }
    static constraints = {
    }


    static mapping = {
        //user column:'createdBy'
        version false

    }
}

Subscription.groovy

class Subscription {

Date dateCreated

static  belongsTo =[topic:Topic,user:User]

enum Sereousness{
    Sereous,VerySereous,Casual
}

static constraints = {
}
static mapping = {
    user column:'user'
    topic column: 'topic'
    version false
}
}

订阅和资源似乎没有关联。据我所知,在 hql 中你不能对不相关的实体使用连接。对于这个你需要交叉连接。 Hibernate 文档第 16.2 和 16.3 章列出了几个示例 http://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch16.html

或者你可以使用hibernate原生查询 https://dzone.com/articles/grails-goodness-using