Can anyone please help me.I have two domains.
Class Parent{
static hasMany = [child: Child];
}
Class Child{
}
In database there are 20 records.I want to get 10 records only without refreshing the page.So i used remote pagination like.
gsp code is
<util:remotePaginate controller="Parent" action="show"
total="${parentList.size()}"
update="updatedListId" max="10" pageSizes="[10, 20, 50,100]"/>
In Controller i wrote like.
def parent =Parent.get(1);
def parentList = parent.getChild();
I tried this one but its is not working.
def childs = Child.findAllByParent(parent, [max: 10])
It is giving all records but i need to here get only 10 records. I set params max value and pass it as argument but its not working.
please help me.
thanks
You can get the childs in a join via HQL:
Child.executeQuery("select c from Parent p join p.childs c where p=:p", [p:p, max:10])
Have you tried where query?
// Parent.groovy
class Parent{
static hasMany = [ children: Child ]
}
// Where query in controller/service
Parent.where { id == 1L }.children.list(max: 10)
Related
I am new to Grails , I have some issue as stated below.
I have 2 domain classes : Parent and Child. I am storing arrays of arrays into database.
Parent class is :
class Parent{
static hasMany = [child: Child]
}
Child class is :
class Child {
String time
String record
String value
static belongsTo= [parent: Parent]
static constraints = {
time(blank: false)
record(blank: false)
belongsTo(blank: false)
}
}
Now my requirement is :
I need to retrieve the child's latest records that contain multiple rows in the database with unique parent id.
e.g : Parent's latest id is 7.
Child table contains nearly 10 records on parent id 7. I want to retrieve all these 10 records with the reference of the parent id(7).
Please some one help to write a code/query .
gorm is a greate ORM you could use following:
def parent = Parent.get(7)
def childList = Child.findAllByParent(parent);
read this, it'll help you to understand gorm better.
I have the following domain classes
class Child{
def age
}
class Parent{
static hasMany = [children:Child]
}
and I would like to execute the following in HQL
Parent.list()
.sort{ parent -> parent.children.sort{ child -> child.age}[0]}[0..10]
Basically I would like to retrieve a list of parents sorted by the age of their eldest child. And restrict this to only 10 records. I don't want to pull all parent and child records from the database, and then do the necessary sorting. I was hoping that HQL could do this on the 'database layer', and only return the results I need. Thanks :)
Parent.withCriteria {
createAlias('children', 'ch', org.hibernate.criterion.CriteriaSpecification.LEFT_JOIN)
order("ch.age", "desc")
maxResults(10)
setResultTransformer(org.hibernate.criterion.CriteriaSpecification.DISTINCT_ROOT_ENTITY)
}
I have a table called employee and child table address.
Now I want to get a list of employees sort by address1 in address table using GORM.
Employee.findAllByName(name, [max: maxRecords, offset: 100,sort: Address.address1, order: desc])
the above statement is not working, any suggestions would be appreciated.
Thanks
Try using a criteria query like so...
def c = Employee.createCriteria()
def results = c.list (max: maxRecords, offset: 100) {
eq("name", name)
address {
order("addres1", "desc")
}
}
This works for me!
Another option is to add a default sort order like so...
class Address{
…
static mapping = {
sort address1:"desc"
}
}
However, I always prefer to do things as an 'as-needed' basis rather than define that sorting be done every time even when it may not be needed. U pick. Enjoy!
I'm just trying to do pagination but until now I couldn't make it. I have 2 domain classes and one to many relationship.
class User {
static hasMany = [contacts:Contact]
}
class Contact {
static belongsTo = [ user : User ]
}
I have 20 Contacts.
When I tried to make a query like this :
def maxResult = 20
def startIndex = 0
def contacts = Contact.findAllByUser(user, [max:maxResult, offset:startIndex])
it is not working. Query is working but pagination with gorm is not working. Result is just 1 contact object.
when I tried;
def startIndex = 0
def contacts = Contact.findAllByUser(user, [offset:startIndex])
Result is 20 contact object but when i tried it with different startIndex value, it is not working also. for startIndex = 5, result is also 20 ontact object.
Is there anybody have any idea about this. Maybe i am doing something wrong, maybe it is gorm's problem. I havent found the answer. Thanks for your answers.
I haven't tried the DynamicFinder to do this thing yet, but as I view the document, your syntax seems to be right. As an alternative, I use createCriteria to solve the paging problem.
def queryResult = Contact.createCriteria().list(max: max, offset: offset) {
and {
/// FILTER ///
user {
eq("id", userInstance.id)
}
}
}
i have domain classes:
package test
class Credit {
String name;
static hasMany = [debts : Debt]
static constraints = {
}
}
and
package test
class Debt {
Integer amount;
Date date;
static belongsTo =[credits: Credit]
static constraints = {
}
}
Need: select max: 10; order: "desc"; sort: "date" rows of Debt associated with the Сredit.get(id)
How can i do it?
solution:
Debt.findAllByCredits(Credit.get(params.id),[max:10, sort:"date",order:"desc"])
but next question about this example:
why, this code work:
def ok = Debt.findAllByCredits(Credit.get(params.id),[max:10, sort:"date",order:"desc"])
println "true:" + ok
but this code not work correct:
def dd = new Debt(credits: Credit.get(params.id))
def wrong =Debt.findAll(dd)
println "no: "+ wrong
all time return all records in table, why?
you could do something like
def hql = "select d from Debt d where credits = ? order by d.date desc"
Debt.findAll(hql, [credit], [max:10])
you might have to tweak that, but something similar should work. Also note, I am assuming that you have an instance of credit that is the parent of the Debts.
You can also use the methods Grails generates dynamically at runtime, based on the properties of your classes
Debt.findAllByCredit(credit, [max:10,sort:"date",order:"desc"]
again, you will need a reference to a credit object.
Check out the docs at
http://grails.org/doc/latest/
specifically the section on findAll and findAllBy under Domain Classes in the left hand nav.