Grails createCriteria 多对多
想象一下我有以下内容(这是我网站的搜索机制)
class Supermarket {
String sp_name
String sp_street
}
class Products {
String p_name
String p_price
}
class products_supermarket{
Supermarket sp
Products pro
}
现在我想创建一个条件:
def c = Supermarket.createCriteria()
def results = c.list {
like("sp_street", params.street)
and {
************ ... params.product
}
maxResults(10)
}
在我有 * 的地方,我希望能够在 products_supermarket 类上找到 supermade 搜索的产品。怎么做呢?
附言。如果 criteria 作为each() 方法工作,迭代所有超市,我可以使用 if-else 语句来搜索产品,如果找到,我可以使用:idEq(it)< /strong>,其中 it 是超市 ID。这样我就能做到。我的问题是,我不知道如何获取当前的 SM 超市 ID。有什么帮助吗?
Imagine i have the following (this is a search mechanism for my website)
class Supermarket {
String sp_name
String sp_street
}
class Products {
String p_name
String p_price
}
class products_supermarket{
Supermarket sp
Products pro
}
Now i want to create a criteria:
def c = Supermarket.createCriteria()
def results = c.list {
like("sp_street", params.street)
and {
************ ... params.product
}
maxResults(10)
}
Where i have the * i want to be able to find products whithin that supermaked searching on products_supermarket class. How to do that?
PS. If criteria works as an each() method, iterating over all supermarkets, i could use an if-else statment to search for products, and if found i could use: idEq(it), where it is the supermarket id. This way i would make it. My problem is, i dont know how to get current'sm supermarket id. Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
and
应用于其中的条件,因此没有必要将其应用于单个语句。默认情况下,顶级标准是和
编辑的。hasMany: Supermarket
和hasMany: Product
即可。连接器表将由 Hibernate 自动生成。ProductsSupermarket
连接器类,请将belongsTo: Supermarket
和belongsTo: Product
添加到该类,并将“hasMany: ProductsSupermarket”添加到另外两个,否则您将失去 Grails 的 GORM 优势。id
非常简单:mySupermarket.id
,或mySupermarket.ident()
(如果关键字段的命名不同)。id
字段默认自动添加到类和表中。所以查询是:
and
is applied to criterias inside it, so there's no point applying it to a single statement. Top-level criterias areand
-ed by defauilt.hasMany: Supermarket
andhasMany: Product
in domain classes. Connector table will be auto-generated by Hibernate.ProductsSupermarket
connector class, do addbelongsTo: Supermarket
andbelongsTo: Product
to it class, and add 'hasMany: ProductsSupermarket' to other two, or you're losing Grails' GORM advantage.id
is as simple as that:mySupermarket.id
, ormySupermarket.ident()
if key field is named differently.id
field is auto-added to class and table by default.So the query is: