【问题标题】:Grails DetachedCriteria doesn't contain sqlRestrictionGrails DetachedCriteria 不包含 sqlRestriction
【发布时间】:2013-07-04 11:06:53
【问题描述】:

我需要将子查询添加到grails.gorm.DetachedCriteria

我尝试通过另一个grails.gorm.DetachedCriteria 来执行此操作,但在这种情况下,我发现grails.gorm.DetachedCriteria 不包含方法sqlRestriction()

我还尝试使用instance.add(Subqueries.exists(subquqery)) 和休眠org.hibernate.criterion.DetachedCriteria 添加子查询,这种方式在我使用CriteriaBuilder 时有效,但在grails.gorm.DetachedCriteria 中它不起作用,因为grails.gorm.DetachedCriteria不包含@987654330 @变量。

谁能帮帮我?

def result = DomainClass1.createCriteria().buildCriteria {
    //some other conditions...
    def subquery1 = DomainClass1.where {
        //some other conditions...
        def subquery2 = DomainClass2.where {
            projections {
                distinct 'id'
            }
            sqlRestriction 'timestamp < to_date(${date},'YYYYMMDDHH24MISS')'
        }
        eqAll 'id', subquery2
    }
    eqAll 'id', subquery1
}.list()

【问题讨论】:

  • 你能发布你的代码吗?
  • 当使用sqlRestriction 时,请确保您引用的是实际的表列名而不是域类属性名。
  • @dmahapatro 当然,但问题是grails.gorm.DetachedCriteria 不包含此方法,我不知道如何避免它。

标签: grails grails-orm


【解决方案1】:

我认为你可以在不使用sqlRestriction 的情况下实现你想要的。

def result = DomainClass1.createCriteria().buildCriteria {
    //some other conditions...
    def subquery1 = DomainClass1.where {
        //some other conditions...
        def subquery2 = DomainClass2.where {
            //Assuming "timestamp" is the domain class property 
            //and not the column name and variable "date" is
            //a string formatted date with the format 'yyyyMMddHHmmss'
            timestamp.before(Date.parse('yyyyMMddHHmmss', date))
            projections {
                distinct 'id'
            }
            //sqlRestriction 'timestamp < to_date(${date},'YYYYMMDDHH24MISS')'
        }
        eqAll 'id', subquery2
    }
    eqAll 'id', subquery1
}.list()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多