gpt4 book ai didi

sql - grails-具有2列的自定义userType和查询

转载 作者:行者123 更新时间:2023-12-02 14:33:47 24 4
gpt4 key购买 nike

假设我有一个POJO,代表坐标(纬度和经度),而 Realm 类则代表地点。地方有座标。

坐标法

class Coordinate implements Serializable{
float latitude
float longitude
...
}

CoordinateUserType.groovy
class CoordinateUserType implements UserType{

int[] sqlTypes() { [Types.FLOAT, Types.FLOAT]}

public Object nullSafeGet(rs, names, owner){
/*not full code published, just what is relevant*/
return new Coordinate(rs.getFloat(names[0]), rs.getFloat(names[1]))
}
public Object nullSafeSet(st, object, index){
/*not full code published, just what is relevant*/
st.setFloat(index, value.latitude)
st.setFloat(index+1, value.longitude)
}


}

广场
class Place{
...
Coordinate coordinate
...
static mapping = {
coordinate type:CoordinateUserType, {
column name:'latitude'
column name:'longitude'
}
}

}

我为坐标(2列)创建了一个userType,并在places域类的映射中引用了它。

我可以正确创建和列出地点。

但我想根据其坐标查询地点,例如

与哪里:
def matchingPlaces = Place.where{
coordinate.latitude > 0 && coordinate.latitude < 10 &&
coordinate.longitude > 0 && coordinate.longitude < 10
}

有条件:
def matchingPlaces = Place.createCriteria().list{
and{
between('coordinate.latitude', 0,10)
between('coordinate.longitude', 0,10)
}
}
/*or */
def matchingPlaces = Place.createCriteria().list{
coordinate{
and{
between('latitude', 0,10)
between('longitude', 0,10)
}
}
}

但是无论我在之间的闭包的列名中键入什么,我总是会收到错误“无法解析属性”。

查询具有多列的自定义用户类型的过程是什么?

提前致谢

最佳答案

只需在embed中使用Place即可:

static embedded = ['coordinate']

您不需要自定义映射,然后也可以删除 CoordinateUserType

然后,将执行以下操作:
def matchingPlaces = Place.createCriteria().list{
coordinate{
and{
between('latitude', 0,10)
between('longitude', 0,10)
}
}
}

关于sql - grails-具有2列的自定义userType和查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19115159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com