【问题标题】:fail while creating HQL of join in Grails在 Grails 中创建连接的 HQL 时失败
【发布时间】:2015-11-13 16:36:49
【问题描述】:

我们必须达到的目标-

用户订阅的主题,按主题中输入的最新帖子排序。

我做了什么-我在 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)");

这是抛出异常/错误消息-

意外标记:(在第 1 行附近,第 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 显示的错误消息。 以下是模型类/实体-

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
}
}

【问题讨论】:

    标签: java mysql hibernate grails hql


    【解决方案1】:

    看起来订阅和资源没有关联。据我在 hql 中所知,您不能将 join 用于未关联的实体。为此,您需要交叉连接。 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      相关资源
      最近更新 更多