# Grails 1.1 条件查询 - 查询关联

**URL:** <https://www.isharkfly.com/t/grails-1-1/1845>\
**Category:** 程序语言\
**Created:** [November 4, 2012, 4:48am UTC](https://www.isharkfly.com/t/grails-1-1/1845 "2012-11-04T04:48:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [November 4, 2012, 4:48am UTC](https://www.isharkfly.com/t/grails-1-1/1845/1 "2012-11-04T04:48:48Z")

</div>

关联可以通过使用一个跟关联属性同名的节点来查询. 比如我们说 Account 类有关联到多个Transaction 对象:`class Account {
	…
	def hasMany = [transactions:Transaction]
	Set transactions
	…
}`我们可以使用属性名 transaction 作为builder的一个节点来查询这个关联:`def c = Account.createCriteria()
def now = new Date()
def results = c.list {
	transactions {
		between('date',now-10, now)
	}
}`上面的代码将会查找所有过去10天内执行过 transactions 的 Account 实例. 你也可以在逻辑块中嵌套关联查询:`def c = Account.createCriteria()
def now = new Date()
def results = c.list {
or {
	between('created',now-10,now)
	transactions {
		between('date',now-10, now)
	}
}
}`这里,我们将找出在最近10天内进行过交易或者最近10天内新创建的所有用户.
