I have a domain like this
Employee {
Address address
String name
String title
}
and another domain
Address {
String country
String locality
String city
}
and now i want to find the all the employee with given city something like that i tried
def employees = Employee.where {
address {
city == params.city
}
}
but this is not working , how can i achieve this
You can write it like following
List<Employee> employees=Employee.createCriteria().list{
'address'{
eq('city',params.city)
}
}
Note: When the eq('city',city) (same parameter name) is used, it will not work.
The syntax for accessing properties of associations differs between criteria and where queries.
Where query
def employees = Employee.where {
address.city == params.city
}
See https://grails.github.io/grails-doc/latest/guide/GORM.html#whereQueries
Criteria query
def employees = Employee.withCriteria {
address {
eq('city', params.city)
}
}
See https://grails.github.io/grails-doc/latest/guide/GORM.html#criteria
Related
class Client {
String name
static hasMany = [courses:Course]
}
class Course {
String name
static belongsTo = [client:Client]
}
I have this and I want to get all Clients that has a Course with name = "blabla"
I was trying to do : Clients.findWhere(Course.any { course -> course.name = "math" })
You can do this with criteria:
Client.withCriteria {
courses {
eq('name', 'math')
}
}
I believe that the following where query is equivalent to the above criteria:
Client.where { courses.name == 'math' }
or you may find you need another closure:
Client.where {
courses {
name == 'math'
}
}
but I rarely use where queries myself so I'm not 100% sure of that.
There are probably a lot of different syntactical expressions to achieve the same thing. I can say definitively that this works in my project though.
def ls = Client.list {
courses {
eq('name','math')
}
}
I have a Grails application and want to create filters for my domain class using named query.
I have domains Act and Status, StatusName is an Enum:
class Act {
static hasMany = [status : Status]
}
class Status {
Date setDate
StatusName name
static belongsTo = [act : Act]
}
I want to filter Acts which have their most recent Status's name equal to specific name.
For now I have this code in Act:
static namedQueries = {
filterOnStatus { StatusName s ->
status {
order('setDate', 'desc')
eq 'name', s
// I need only first Status, with most recent setDate
// among all Statuses of that Act
}
}
}
But this filter all Acts that have Status with specific name, not only with most recent. I tried to place maxResult 1 in query, but it seems not to work.
Any help would be appreciated.
EDIT: Problem was solved that way:
filteronStatus {
createAlias('status', 's1')
eq 's1.name', s
eq 's1.setDate', new DetachedCriteria(Status).build {
projections {
max('setDate')
eqProperty('act', 's1.act')
}
}
}
see 'namedQueries' from Grails Doc
// get a single recent Act
def recentAct = Act.filterOnStatus(statusName).get()
ADD:
HQL
"select s1.act from Status as s1 \
where s1.name = :statusName \
and s1.setDate = (select max(s0.setDate) from s1.act.status s0)"
NamedQuery
listByStatus { statusName ->
createAlias('status', 's1')
eq 's1.name', statusName
eq 's1.setDate', new DetachedCriteria(Status).build{ projections { max('setDate')} eqProperty('act','s1.act') }
}
I have a Domain class Supplier:-
class Supplier {
static embedded=['address']
static constraints = {
vendorName(blank:false,nullable:false,maxSize:50)
address(blank:false,nullable:false,unique:true)
contactPerson(blank:false,nullable:false,maxSize:50)
}
String vendorName
Address address
String contactPerson
}
and the Address class:-
class Address {
String street
String area
static constraints = {
street(blank:false,nullable:false)
area(blank:false,nullable:false)
}
}
My requirement is to check for the uniqueness of street in supplier. when a user enter street and area from Supplier view, i have to check that street should be unique for a vendor.
thnks
It will be like that if only street should be unique
class Address {
String street
String area
static constraints = {
street(blank:false,nullable:false)
area(blank:false,nullable:false)
}
static mapping = {
street(index: true, indexAttributes: [unique: true])
}
}
Since you only have one Address per Supplier street is already unique per Supplier. If you can't have more than 1 address you can't have duplicate streets.
class Contact {
String name
String number
}
class Message {
String text
String number
Contact contactInfo //If any
}
I need to join on Message.number = Contact.number. Any thoughts on creating association in Grails/GORM with non primary key column?
I'm pretty sure this isn't possible in GORM, and I don't know if it's even possible in regular Hibernate. But you can fake it:
class Message {
String text
String number
static transients = ['contactInfo']
Contact getContactInfo() {
Contact.findByNumber(number)
}
void setContactInfo(Contact contact) {
number = contact.number
}
}
Burt, this is possible with hibernate using the property-ref attribute
in my domain model, I have a method that does something with my data.
e.g.
class Person {
String lastname
String firstname
String bigname() {
return lastname.toUpperCase()
}
static namedQueries = {
withBigname { name ->
eq(this.bigname(), name)
}
}
}
I want to use this method like a property in the named query, but this.bigname() only throws a java.lang.IncompatibleClassChangeError-Exception.
Does anyone know how to use domain methods in criteria and named queries?
Update: I now tried this:
class Person {
String lastname
String firstname
String bigname
static transients = [ 'bigname' ]
def getBigname() {
return lastname.toUpperCase()
}
static namedQueries = {
withBigname { name ->
eq('bigname', name)
}
}
}
But it only results in a "could not resolve property: bigname"-exception...
You cannot use class methods in queries, because queries are actually translated to SQL.
You might be able to get what you need by using writing the complexity in SQL a "SQL Restriction". Search for "SQL Restriction" on http://grails.org/doc/2.0.x/guide/GORM.html
HTH
Try to name the method in JavaBean getter and setter notation.
Rename method bigname() to getBigname().
On a static closure, you don't have a this. You'll either need to store the bigname in the Database, do some sort of case insensitive criteria.
looks like you are trying to accomplish this:
class Person {
String lastname
String firstname
static namedQueries = {
withName { name ->
eq('lastname', name, [ignoreCase: true])
}
}
}