class Car {
String carname
}
class Factory {
Car car
String factoryname
}
How can I check if an object Factory with factoryname == "Kia", carname == "newmodel" exists?
You could use createCriteria() method and alias.
def cName = 'newmodel'
def fName = 'Kia'
def objects = Factory.createCriteria().count() {
createAlias('car', 'c'),
eq('c.carname', cName)
eq('factoryname', fName)
}
boolean isThere = objects
Related
I have 3 classes
class Company {
String company_name
}
class Job {
String job_name
Company company
}
class Person {
String person_name
Job job
}
How can I check if a position is really existing from an existing company and it is vacant (no Person is on it) or in other words there is existing Job object with existing Company of course but no Person has in his constructor passed this Job object, here is what I have done,where is my mistake ?
if (person==null && job != null && company != null)
{
def query=Job.where{company.company_name == company_name && job_name == job_name}
def query2=Person.where {job.job_name == job_name}
if( query == null && query2 != null )
{
def person12 = new Person(job: job, person_name: person_name)
if (person12.validate() && person12.save())
{
redirect(url: "https//localhost:8080")
}
}
I would relate the classes in other way if possible:
class Company {
String company_name
static hasMany = [jobs: Job]
}
class Job {
static belongsTo = [company: Company]
static hasOne = [person: Person]
String job_name
}
class Person {
String person_name
static belongsTo = [job: Job]
}
And then you can create a criteria to know if a job has no person related.
List<Job> jobs = Job.withCriteria{
eq('company', companyObject)
isNull('person')
}
You don't need to validate an object before save it, cause it automatly validated before, and if it is not valid, return false.
if (person12.save()) {
redirect(url: "https//localhost:8080")
}
I have two domains
class ProductQuantity {
Integer quantity
static belongsTo = [productSize: ProductSize]
}
class ProductSize {
String size
static hasMany = [productQuantities : ProductQuantity]
}
I'm trying to build a query where I get all ProductQuantity by the productSize. I have the following query that works.
def productSize = ProductSize.findAllById(1);
def productQuantities = ProductQuantity.findAllByProductSize(productSize)
I'm looking to get the ProductQuanties in a single query rather than two separate queries.
ProductQuantity.createCriteria().list {
eq 'productSize', ProductSize.load(1)
}
or
ProductQuantity.withCriteria {
eq 'productSize', ProductSize.load(1)
}
or
ProductQuantity.where {
productSize == ProductSize.load(1)
}.list()
or
ProductQuantity.findAll("from ProductQuantity where productSize = ?", [ProductSize.load(1)])
Yes, you can get this by createCriteria, like --
def productQuantities = ProductQuantity.createCriteria().list() {
productSize {
eq('id', 1)
}
}
If I have two domain classes like this:
class Company{
string Name
string address
}
class User {
string firstName
string lastName
Company company
}
How can I get all the users from company named Google using criteria query? Something like this:
def company = Company.findByName("Google")
def c = User.createCriteria()
def usersByCompany = c.list {
eq("company", company)
}
You can declare a block inside your closure to filter any field in the Company:
def usersOfGoogle = User.createCriteria().list() {
company {
eq('name', 'Google')
}
}
I just don't remember if it works only for relationships (belongsTo & hasMany), maybe you will need to change your domain class:
class User {
static belongsTo = [company : Company]
}
Is there an easy way to deserialize a JSON string to a domain class with support of embedded association; belongsTo and hasMany
{
name: "Customer",
contact: {
name: "Contact"
}
}
class Customer {
name
Contact contact
}
class Contact {
String name
static belongsTo = [customer:Customer]
}
in my controller I would like to do the following
def save() {
def customer = new Customer(request.JSON)
customer.save();
}
Now i'm forced to do
def save() {
def contact = new Contact(request.JSON.contact);
def customer = new Customer(request.JSON);
customer.contact = contact;
customer.save();
}
Have you tried using JsonSlurper?
Example usage:
def slurper = new JsonSlurper()
def result = slurper.parseText('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')
assert result.person.name == "Guillaume"
assert result.person.age == 33
assert result.person.pets.size() == 2
assert result.person.pets[0] == "dog"
assert result.person.pets[1] == "cat"
Ref: http://groovy.codehaus.org/gapi/groovy/json/JsonSlurper.html
you can try this
Test test
def result = new JsonSlurper().parseTest('yourString')
test = result
Try this will work.
My (simplified) domain model looks like this:
class Student {
static hasMany = [professions:StudentProfession];
}
class StudentProfession {
static belongsTo = [student:Student];
Profession profession;
}
class Profession {
String name;
}
What is the most efficient way to:
List all students that are taught "Programmer" and "Manager" professions
Am I forced to filter them out after querying the database?
students = students.findAll { student ->
student.professions.find { professionNames.contains(it.profession.name) } != null
}
You can do this using a GORM query:
def studends = Student.where {
professions {
profession.name == "Programmer" || profession.name == "Manager"
}
}
Lots of ways to skin this cat - here's one:
StudentProfession.findAllByProfessionInList(Profession.findAllByNameInList(["Programmer","Manager"])*.student.unique()