---
title: "GRAILS belongsTo hasMany 查询问题"
author: "seanwon"
date: "2009-03-24T12:03:31Z"
tags: []
categories: ["企业架构"]
slug: "grails-belongsto-hasmany"
draft: false
lastmod: "2009-03-25T13:26:59Z"
id: 354071
url: "https://seanwon.iteye.com/blog/354071"
---

**这个bug已经在1.1的正式版本中修复。**

举个例子：
*class Author {
  static hasMany = 
}
class Book {
  static belongsTo = 
}*
**查询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 {
}*