【问题标题】:Grails : from HQL to DetachedCriteria QueryGrails:从 HQL 到 DetachedCriteria 查询
【发布时间】:2012-01-20 14:11:49
【问题描述】:

假设我有以下领域模型:

class Book {
  String title
  Set authors

  static hasMany = {authors: Author}
}

class Author {
  String name
}

在给定书名查询的情况下,检索 Author 集合的 HQL 查询:

Author.executeQuery("select distinct author from Book as book join book.authors as author where book.name like ?", ["%groovy%"])

但是我希望能够在 DetachedCriteria 或类似的情况下获得相同的结果(但有可能......?)并且不添加从 Author 到 Book 的关系(否则它会很明显)

谢谢

【问题讨论】:

    标签: grails hql grails-orm detachedcriteria


    【解决方案1】:

    这可以通过条件或分离条件通过几种方式完成,但使用常规 GORM 条件会更容易一些,因为它实现了 Grails 2.2.2 中分离条件不具备的 createAlias 命令:

    Create Alias In Detached Criteria

    这是两种方式:

    package bookauthor
    
    import grails.gorm.DetachedCriteria
    import grails.orm.HibernateCriteriaBuilder
    
    
    class MyController {
    
    def index() {
      HibernateCriteriaBuilder ac2 = Author.createCriteria()
      HibernateCriteriaBuilder criteria2 = Author.createCriteria()
    
      HibernateCriteriaBuilder criteria = Book.createCriteria()
    
      def bookResults = criteria {
        projections {
          property 'aut.id'
        }
        createAlias('authors',  'aut')
        like('title', '%Groovy%')
    
      }
    
      def dc = new DetachedCriteria(Book).build {
        authors {}
        like('title', '%Groovy%')
      }
    
      def myList = dc.list().collect { it.authors.collect { author -> author.id} }.flatten()
    
      def resultsDetached = criteria2 {
        'in'('id', myList )
      }
    
      def results = ac2 {
        'in'('id', bookResults )
      }
    log.info("RESULTS: " + results)
    log.info("Detached RESULTS: " + resultsDetached)
    }  
    
    }
    

    您会在日志中看到:

    bookauthor.MyController  - RESULTS: [bookauthor.Author : 1, bookauthor.Author : 3]
    bookauthor.MyController  - Detached RESULTS: [bookauthor.Author : 1, bookauthor.Author : 3]
    

    【讨论】:

      【解决方案2】:

      不幸的是,AFAIK,这个查询是不可能的。不过,可能会出现以下丑陋的查询:


      select author from Author author
      where author.id in (select author2.id from Book book
                          join book.authors as author2 
                          where book.name like :bookName)
      

      对于这样一个简单的、非动态组合的查询,我会坚持使用您的 HQL 查询。如果你真的需要使用Criteria,那么对应的代码是这样的:


      Criteria c = session.createCriteria(Author.class, "author");
      DetachedCriteria dc = DetachedCriteria.forClass(Book.class, "book");
      dc.createAlias("book.authors", "author2");
      dc.add(Restrictions.like("book.name", bookName));
      dc.setProjection(Projections.property("author.id"));
      c.add(Subqueries.propertyIn("author.id", dc);
      List<Author> authors = (List<Author>) c.list();
      

      【讨论】:

      • 这些是hibernate Criteria和DetachedCriteria查询?不是 GORM Criteria 和 DetachedCriteria 吗?您知道如何使用 de Grails/GORM Criteria 和 DetachedCriteria 编写相同的内容吗?
      • 没有。我认为 GORM 只是 Hibernate 的 Java API 的包装器。
      猜你喜欢
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      相关资源
      最近更新 更多