论坛首页 Java企业应用论坛

GRAILS belongsTo hasMany 查询问题

浏览 2738 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-24   最后修改:2009-03-25
这个bug已经在1.1的正式版本中修复。

举个例子:
class Author {
  static hasMany = [books:Book]
}
class Book {
  static belongsTo = [author: Author]
}

查询Author:
def criteria = Author.createCriteria()
def list = criteria {
   //conditions
}
list.each { author->
  author.books.each{
  }
}

在author.books.each这一步就会导致book的更新(version会变化,有时候会导致乐观锁错误)
改成如下方式可避免:
def books = Book.findAllByAuthor(author)
books.each {
}

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics