简体   繁体   English

用于子查询的Grails Createcriteria

[英]Grails Createcriteria for subquery

How to use createCriteria for subQuery . 如何将createCriteria用于subQuery。 My HQL is like this : 我的HQL是这样的:

def lists = List.executeQuery("FROM List cl WHERE cl.brand_id in (SELECT b.id FROM Brand b WHERE cl.brand_id=b.id AND b.brand_name rLIKE '^[0123456789].*')") def列表= List.executeQuery(“从列表中的FROM列表cl在哪里cl.brand_id(从品牌b中选择b.id,在哪里b.id cl.brand_id = b.id和b.brand_name rLIKE'^ [0123456789]。*')”)

Please tell how can we write this using Createcriteria ?? 请告诉我们如何使用Createcriteria编写此代码?

thanks in advance. 提前致谢。

It seems that Hibernate doesn't support rlike or regexp feature (see http://opensource.atlassian.com/projects/hibernate/browse/HHH-3404 ) But Grails does !! 似乎Hibernate不支持rlike或regexp功能(请参阅http://opensource.atlassian.com/projects/hibernate/browse/HHH-3404 ),但是Grails可以!

For that you need to used createCriteria (and not executeQuery) Based on this discussion http://n4.nabble.com/Using-rlike-in-a-gorm-query-td1391934.html and on http://jira.codehaus.org/browse/GRAILS-3481 , your solution will be: 为此,您需要使用createCriteria(而不是executeQuery)基于此讨论http://n4.nabble.com/Using-rlike-in-a-gorm-query-td1391934.htmlhttp://jira.codehaus .org / browse / GRAILS-3481 ,您的解决方案将是:

def c = List.createCriteria(); 
results = c { 
        rlike("name", "^[0123456789].*") 
} 

Attention: it might work only on MySQL and Oracle 注意:它可能仅适用于MySQL和Oracle

When querying on a domain object's property you need to make a block in the criteria with the name of the property. 查询域对象的属性时,您需要在条件中使用该属性的名称进行屏蔽。 Something like: 就像是:

def criteria = List.createCriteria(); 
def results = criteria {
    brand {
        rlike("name", "^[0123456789].*") 
    }
}

See also the "Querying Associations" section in the Grails GORM criteria docs 另请参阅Grails GORM条件文档中的“查询关联”部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM