beb*_*tie 0 sql grails hibernate hql grails-orm
我试图确定如何有效地找到/检索/加载对象.)最小化对数据库的调用和b.)保持代码尽可能优雅/简单(即不编写hql等).
假设您有两个对象:
public class Foo {
Bar bar
String badge
}
public class Bar {
String name
}
Run Code Online (Sandbox Code Playgroud)
每个Foo都有一个酒吧和徽章.还假设所有徽章在条形图中都是唯一的.因此,如果Foo的徽章为"4565",则没有其他Foos具有相同的徽章#和相同的栏.
如果我有一个酒吧ID,我怎样才能有效地检索Foo而不先选择Bar?
我知道我可以这样做:
Foo.findByBadgeAndBar("4565", Bar.findById("1"))
Run Code Online (Sandbox Code Playgroud)
但这似乎导致Bar表上的select,然后是Foo表上的select.换句话说,我需要生成以下Grails/Hibernate/GORM:
select * from foo where badge="4565" and bar_id="1"
Run Code Online (Sandbox Code Playgroud)
你可以使用标准
def c = Foo.createCriteria()
def results = c {
eq("badge", "4565")
eq("bar.id", 1L)
}
Run Code Online (Sandbox Code Playgroud)
这会产生一个select语句