[英]Grails GORM find by value inside map of Domain
I am using Grails 2.2.4 and having one Domain contains value as map and I want to find domain object using key of map. 我正在使用Grails 2.2.4,并且有一个Domain包含作为地图的值,并且我想使用map的键查找领域对象。 Please help me to resolve this issue. 请帮助我解决此问题。
Student.groovy
package com.grails
import java.util.Map;
class Student {
String firstName
String lastName
Map address
static constraints = {
}
}
When My application are run I can see that Grails application create tables in database are as follow: 当运行我的应用程序时,我可以看到Grails应用程序在数据库中创建表如下:
1) first table
student
id
version
first_name
last_name
indexes
2) second table
student_address
address
addres_idx
addres_elt
When I save Domain as: 当我将域另存为时:
def std = new Student()
std.firstName = 'Piyush'
std.lastName = 'Chaudhari'
std.address = [city:'Surat',state:'Gujarat',pincode:'38001']
std.save(flash:true)
values are insert in database as follow: 值将插入数据库中,如下所示:
student table
ID VERSION FIRST_NAME LAST_NAME
1 0 Piyush Chaudhari
student_address table 学生地址表
ADDRESS ADDRESS_IDX ADDRESS_ELT
1 city Surat
1 state Gujarat
1 pincode 38001
Now, I want data or row using GORM like Student.findBy_____ or Student.findAllBy______ where 'city' = surat 现在,我想使用GORM的数据或行,例如Student.findBy _____或Student.findAllBy ______,其中“ city” = surat
Any one can help me to resolved this issue? 谁能帮助我解决这个问题?
You can use: 您可以使用:
Student.findBy<FieldName1>And<FieldName2> (<FieldNameParameter1>, <FieldNameParameter2>)
Or Either:` 或者:
Student.list().find { it.address.city == 'Surat' }
Student.list().findAll { it.address.city == 'Surat' }
` `
Define an address class, and then add an address field to the student class (this will change how your tables are mapped in the database): 定义一个地址类,然后在学生类中添加一个地址字段(这将改变您的表在数据库中的映射方式):
class Student {
String firstName
String lastName
Address address
static constraints = {
}
}
class Address {
String city
String state
String pincode
}
Address should be another entity in your domain, not a map of values. 地址应该是您域中的另一个实体,而不是价值映射。 Remember that Grails GROM is an ORM, so you should design your domain using a OOP model in order to take advantage of the dynamic finders and criterias for doing queries. 请记住,Grails GROM是一个ORM,因此您应该使用OOP模型设计域,以便利用动态查找器和条件进行查询。
With those changes in place, you can now use a simple criteria: 完成这些更改后,您现在可以使用简单的条件:
def students = Student.withCriteria{
'address'{
eq('city', 'surat')
}
}
More information about criterias in the grails docs: http://grails.org/doc/latest/ref/Domain%20Classes/withCriteria.html http://grails.org/doc/latest/guide/single.html#criteria 有关grails文档中条件的更多信息,请访问: http : //grails.org/doc/latest/ref/Domain%20Classes/withCriteria.html http://grails.org/doc/latest/guide/single.html#criteria
If you want to use Dynamic finders, you will have to get all the address with city = 'surat' and then use a findByAddressInList(...). 如果要使用动态查找器,则必须使用city ='surat'获取所有地址,然后使用findByAddressInList(...)。 But i think that in this case, criterias is a better approach 但我认为在这种情况下,标准是一种更好的方法
I don't think that you can search things like this using maps. 我认为您无法使用地图搜索类似的内容。
Maybe you can do this: 也许您可以这样做:
def students = Student.list()
def result = students.each { student -> student.address.city == 'Surat' }
println("Resultado" + result)
But this is a very bad way to do this kind of things 但这是做这种事情的非常不好的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.