I want to retrieve the 10 nearest geolocalized objects using Gorm.
To do so, I would like to customize the order() parameter in order to use a get_distance(longitude, latitude, :longitude, :latitude) sql function.
I've been struggling with this all day, does anybody have a hint ?
Thanks !
EDIT
I finally managed to do what I wanted but with a very ugly solution :
I added a sqlProjection in which I put my get_distance() function and was able to order by it.
Adding a projection removes the retrieval of the object properties so I had to explicitly ask for it by adding property projections and I managed to do it with introspection.
Then I had to define the result transformer of the criteria for it to give me domain instances.
If register your function with the hibernate SQL dialect, then you can use it in HQL queries. For example, put this in your bootstrap code:
import org.hibernate.dialect.function.SQLFunctionTemplate
import org.hibernate.Hibernate
def dialect = applicationContext.sessionFactory.dialect
def getDistance = new SQLFunctionTemplate(Hibernate.INTEGER, "get_distance(?1,?2)")
dialect.registerFunction('get_distance', getDistance)
Then you can use it in HQL:
Location.executeQuery(
"""
select id, get_distance(latitude, longitude) as distance
from Location
order by distance
""",
[], [max: 10])
What we did is the following:
Create the query with whatever DB specific operators, but as a view
Create a new domain class object solely to map to the view, thereby allowing you to do GORM criteria queries and the like
Related
I have the following model:
1 Person to many Participant ('enrollments' is the navigator)
If I just want the name of the person, and the date of all enrollments, this doesn't work:
EntityQuery.from('Person').where('id','eq',id)
.select('firstName, lastName, enrollments.dateEnrolled')
.execute()
I get an error:
Unable to locate property 'DateEnrolled' on type
'System.Collections.Generic.ICollection`1[EntityClasses.Participant]'
What is the proper syntax for projections of fields on one-to-many relationships?
There is no really good way to express this in a single query. You can get all of the "enrollments" but you will need to select out the "dateEnrolled" on the client.
EntityQuery.from('Person').where('id','eq',id)
.select('firstName, lastName, enrollments').expand("enrollments")
.execute().then(function(data) {
// extract "dateEnrolled" from data.results
});
Alternatively, you can use a 'named query" on the server that performs the projection there and simply query the projection from the client.
Or perhaps better would be to invert your query. Something like this: ( assuming you have a scalar property named 'Person' on your 'Enrollment' type).
EntityQuery.from('Enrollments').where('person.id','eq',id)
.select('person.firstName, person.lastName, dateEnrolled').expand("person")
.execute().then(...);
I got this working on my fork of breeze.server.net: https://github.com/eggers/breeze.server.net/tree/property-of-collection-25
Grails gives the possibility of creating simple string/value map properties section "Maps of Objects", first paragraph.
I was wondering, is there a way to later query the domain class (using Gorm dynamic finders, criterias or HQL) using the map property as part of the query (i.e adding a condition for the key X to have the value Y)?
After playing with it a bit and almost give up, I discovered the map syntax to (surprisingly) work in HQL. Assuming the class looks like:
class SomeClass {
Map pairKeyProperty
}
You can build queries that look like the following:
select * from SomeClass sc where sc.pairKeyProperty['someKey'] = 'someValue' and sc.pairKeyProperty['someOtherKey'] = 'someOtherValue'
Pretty neat! I still would prefer to use criterias as they are much cleaner to compose, but they seem to not support the same syntax (or I couldn't find it).
I created a sample app in GitHub:
https://github.com/deigote/grails-simple-map-of-string-value-pairs
It can be visisted at:
http://grails-map-of-string-pairs.herokuapp.com/
The form above uses a cross join. To enforce an inner join use
join sc.pairKeyProperty pk1 on index(pk1) = 'someKey'
where 'someValue' in elements(pk1)
I have two object, a room type and a reservation. Simplified they are:
class Room {
String description
int quantity
}
class Reservation {
String who
Room room
}
I want to query for all rooms along with the number of rooms available for each type. In SQL this does what I want:
select id, quantity, occupied, quantity-coalesce(occupied, 0) as available
from room left join(select room_id, count(room_id) as occupied from reservation)
on id = room_id;
I'm not getting anywhere trying to work out how to do this with HQL.
I'd appreciate any pointers since it seems like I'm missing something fairly fundamental in either HQL or GORM.
The problem here is your trying to represent fields that are not your domain classes like available and occupied. Trying to get HQL\GORM to do this can be a bit a little frustrating, but not impossible. I think you have a couple options here...
1.) Build your domain classes so that there easier to use. Maybe your Room needs to know about it's Reservations via a mapping table or, perhaps write what you want the code to look like and then adjust the design.
For example. Maybe you want your code to look like this...
RoomReservation.queryAllByRoomAndDateBetween(room, arrivalDate, departureDate);
Then you would implement it like this...
class RoomReservation{
...
def queryAllByRoomAndDateBetween(def room, Date arrivalDate, Date departureDate){
return RoomReservation.withCriteria {
eq('room', room)
and {
between('departureDate', arrivalDate, departureDate)
}
}
}
2.) My second thought is... It's okay to use the database for what it's good for. Sometimes using sql in you code is simply the most effective way to do something. Just do it in moderation and keep it centralized and unit tested. I don't suggest you use this approach because you query isn't that complex, but it is an option. I use stored procedures for things like 'dashboard view's' that query millions of objects for summary data.
class Room{
...
def queryReservations(){
def sql = new Sql(dataSoruce);
return sql.call("{call GetReservations(?)}", [this.id]) //<-- stored procedure.
}
}
I'm not sure how you can describe a left join with a subquery in HQL. INn any case you can easily execute raw SQL in grails too, if HQL is not expressive enough:
in your service, inject the dataSource and create a groovy.sql.Sql instance
def dataSource
[...]
def sql= new Sql(dataSource)
sql.eachRow("...."){row->
[...]
}
I know it's very annoying when people try to patronize you into their way of thinking when you ask a question, instead of answering your question or just shut up, but in my opinion, this query is sufficiently complex that I would create a concept for this number in my data structure, perhaps an Availability table associated to the Room, which would keep count not only of the quantity but also of the occupied value.
This is instead of computing it every time you need it.
Just my $.02 just ignore it if it annoys you.
I was wondering if it was possible to create a SPARQL UpdateRequest in Jena by using ARQ Op objects. I would be interested to create programmatically updates like this:
DELETE {?s :predicate <http://example.org#old> }
INSERT {?s :predicate <http://example.org#toAdd>}
WHERE {?s :predicate <http://example.org#old> }
by creating the patterns in the DELETE, INSERT, and WHERE clauses from the ARQ API.
So far the only ways I have found to create SPARQL Update requests require to parse a SPARQL string or to create a com.hp.hpl.jena.update.Update object (which uses QuadAcc objects for which I couldn't find examples of use.
My fear is that the management of SPARQL UPDATE requests and the one of SPARQL SELECT queries are separated and that ARQ cannot be used to 'assemble' queries on the fly.
Thanks in advance
This question also burned me. I wanted to compose an UpdateRequest from ElementGroup objects and ElementTriplesBlock objects. This are the two main classes used to construct a Query. For example:
ElementGroup queryPattern = ...
ElementTriplesBlock constructTriples = ...
Query query = new Query();
query.setQueryConstructType();
// set CONSTRUCT clause
query.setConstructTemplate(new Template(constructTriples.getPattern()));
// set WHERE clause
query.setQueryPattern(queryPattern);
I tried the Jena mailing-list and received this answer:
The Update API is designed to deal with streaming arbitrarily large
unbounded INSERT and DELETE data hence the use of QuadAcc rather than
an Element for the INSERT/DELETE portion of the update.
Eventually I implemented this using a ParametrizedSparqlString:
ElementGroup queryPattern = ...
ElementTriplesBlock deleteTriples = ...
ElementTriplesBlock insertTriples = ...
ParameterizedSparqlString qstring = new ParameterizedSparqlString();
// Set DELETE clause
qstring.append("DELETE {");
qstring.append(deleteTriples.toString());
qstring.append("}");
// Set INSERT clause
qstring.append("INSERT {");
qstring.append(insertTriples.toString());
qstring.append("}");
// Set WHERE clause
qstring.append("WHERE {");
qstring.append(queryPattern.toString());
qstring.append("}");
// Construct an update query
UpdateRequest request = qstring.asUpdate();
I haven't tried this myself, but it looks like creating Update objects and assembling them into an UpdateRequest is indeed the way to go.
After a short look, QuadAcc doesn't seem particularly difficult, just use addTriple() with triples that contain variables.
The UpdateModify subclass of Update looks particularly interesting, it corresponds to the DELETE … INSERT … WHERE pattern in your example. Unfortunately the WHERE clause is initialised with an Element (syntactic representation of a query part) rather than an Op (algebraic representation).
I know this is simple question but taking more time
How to find first record from table in grails .
I need to get only the first record with out knowing the id number .
Is there any method like find :first in grails ?
thanks in advance .
Updating to Grails 2.1.1 or later adds two new methods (first and last) for GORM to address this needed feature.
From the docs:
class Person {
String firstName
String lastName
Integer age
}
// retrieve the first person ordered by the identifier
def p = Person.first()
// retrieve the first person ordered by the lastName property
p = Person.first(sort: 'lastName')
// retrieve the first person ordered by the lastName property
p = Person.first('lastName')
Well, you have to define by what measure this record is supposed to be the "first".
Assuming that you mean the record with the earliest creation timestamp, the easiest and most robust approach would be to add a dateCreated property to your domain class and then querying for the entity with the lowest such date. In fact you don't even have to set the creation date manually, because Grails does this for you (as long as you name the property dateCreated) - see Automatic timestamping in the Grails Documentation.
The HQL query would be something like:
def firstObject = YourClass.find("FROM YourClass ORDER BY dateCreated")
Check out hibernate criteria and projections, e.g:
def location = Location.createCriteria()
def firstRecord = location.list{
maxResults(1)
order("id","asc")//assuming auto increment just to make sure
}[0]
http://grails.org/doc/1.0.3/ref/Domain%20Classes/createCriteria.html
If timestamp doesn't matter, you could try if Daniel's answer without ORDER BY works, i.e.
def firstObject = YourClass.find("FROM YourClass")
You can use the grails findBy methods to return the first result of a query.
-> From the 1.3.7 docs
findBy*
Purpose
Dynamic method that uses the properties of the domain class to allow
the creation of Grails query method expressions that return the first
result of the query
-> from the latest docs
findBy* Purpose
Dynamic method that uses the properties of the domain class to execute
a query returning the first matching result.