| Grails版本:3.0.9 | Groovy版本:2.4.5 | JVM版本:1.8.0_60
您好,
我有以下GORM查询,涉及'事件'之间的联接。和' EventCategory'带有页面结果的域对象。
def advancedSearchWithPagedResults(int max, int offset, String search, Date startDate, Date endDate, List myEventCategories) {
// need to convert our list of ints to longs as the field they'll be compared to is a long
def listOfLongs = myEventCategories.collect {
it.toLong()
}
def wildcardSearch = search ? '%' + search + '%' : '%'
def ids = Event.createCriteria().list {
projections {
distinct 'id'
}
maxResults max
firstResult offset
or {
like("eventName", wildcardSearch)
like("address.town", wildcardSearch)
like("address.county", wildcardSearch)
}
and {
if (listOfLongs) {
eventCategories {
'in'("id", listOfLongs)
}
}
between("startDateTime", startDate, endDate)
eq("enabled", true)
}
order("startDateTime", "asc")
}
/* Get the acual events using the list of id's just obtained */
def results = Event.getAll(ids)
return results
}
但是,我需要添加/合并以下MySQL查询,该查询计算每个事件(以英里为单位)与提供的入口和经度(51.519159,-0.133190)之间的距离,然后过滤掉任何超出的事件25英里(在这个例子中)。事件也按照提供的纬度/经度的距离排序。
SELECT
`event`.*,
( 3959 * acos( cos( radians(51.519159) ) * cos( radians( `event`.address_latitude ) )
* cos( radians(`event`.address_longitude) - radians(-0.133190)) + sin(radians(51.519159))
* sin( radians(`event`.address_latitude)))) AS distance
FROM `event`
WHERE `event`.enabled = 1
HAVING distance < 25
ORDER BY distance;
我的问题是如何最好地改变GORM查询以纳入距离计算?
我是否需要抛弃GORM查询并使用本机HQL查询?我不希望。
任何想法都会非常感激。
为了完整起见,我将包含两个域对象的相关部分。
class Event implements Taggable {
static hasMany = [courses: Course,
eventCategories: EventCategory,
faqs: FAQ]
static belongsTo = [eventOrganiser: EventOrganiser]
java.util.Date dateCreated
java.util.Date lastUpdated
boolean enabled = true
String eventName
String organisersDescription
@BindingFormat('dd/MM/yyyy HH:mm')
java.util.Date startDateTime
@BindingFormat('dd/MM/yyyy HH:mm')
java.util.Date endDateTime
@BindingFormat('dd/MM/yyyy HH:mm')
java.util.Date entriesOpenDateTime
@BindingFormat('dd/MM/yyyy HH:mm')
java.util.Date entriesCloseDateTime
BigDecimal fromPrice
Address address
Contact primaryContact
static embedded = ['address','primaryContact']
// transient fields
double distanceFromUsersPostcode
....
}
class EventCategory {
static hasMany = [events:Event]
static belongsTo = [Event]
String parentCategoryName
String parentSubCategoryName
String categoryName
String description
int order
}
答案 0 :(得分:0)
您可以在域类内部将距离查询存储为formula
,然后将其作为该类的属性进行调用。
活动类:
static mapping = {
distance formula: """
(3959 * acos(cos(radians(51.519159)) *
cos(radians(ADDRESS_LATITUDE)) * cos( radians(ADDRESS_LONGITUDE) -
radians(-0.133190)) + sin(radians(51.519159)) * sin( radians(ADDRESS_LATITUDE))))
"""
}
您可能需要尝试使用公式字符串(新行字符是否会导致问题)。
似乎查询的几个部分都是常量,可以将其排除在外:acos(cos(radians(51.519159))
,radians(-0.133190)
,sin(radians(51.519159))
Criteria Builder
您现在可以像使用其他任何属性一样使用distance
属性:
lt('distance', 25)