3

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

我试图通过另一个来做到这一点grails.gorm.DetachedCriteria,但在这种情况下我发现它grails.gorm.DetachedCriteria不包含方法sqlRestriction()

我也尝试通过 usinginstance.add(Subqueries.exists(subquqery))和 hibernate添加子查询org.hibernate.criterion.DetachedCriteria,这种方式在我使用时有效CriteriaBuilder,但grails.gorm.DetachedCriteria 由于grails.gorm.DetachedCriteria不包含instance变量而不起作用。

谁能帮我?

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()
4

1 回答 1

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()
于 2013-07-04T15:02:06.690 回答