Hi I'm trying to perform a sort in a controller of parent and child before rendering a json file but I'm not sure how to go about doing it. Here's what I have (excerpt of original code):
class Parent{
static hasMany = [children:Child]
String name
Date dateCreated
}
class Child {
static belongsTo = [parent:Parent]
String name
Date dateCreated
}
In my controller .groovy file I have :
def list(){
def result = Parent.listOrderByDateCreated(order: "desc")
.... more code ....
withFormat{
json {render result as JSON}
xml {render result as XML}
}
}
and the above works (parent is sorted by date created) but I'm not sure how can I sort all the children by date created within the list.
Thank you for your help in advance. Also I'm using Grails 2.3.2
One way is to assume you always want the children sorted by the dateCreated. Add the following to your Parent domain:
static mapping = {
children sort: 'dateCreated'
}
Another way would be to do the sort after you've pulled the results:
def sortedChildren = parent.children.sort { it.dateCreated }
If there is a fancier "grailsier" way to do this via finders or criteria, I do not know.
Related
i'm making a tables cleaning service that takes the table name and the date field as arguments , here is the service code :
def cleanTables(tableName , dateField) {
def comparisonDate
def numOfRecordsDeleted
use (TimeCategory){
comparisonDate=new Date() -1.year
}
numOfRecordsDeleted=tableName.where { dateField <=comparisonDate }.deleteAll()
log.info("deleted : " +numOfRecordsDeleted)
}
i'm successfully passing to this service the table name but i can't pass the date field , so how to get a specific property from a domain for example a domain named Payments got a property dateCreated , so i pass to my service Payments and dateCreated.
With where queries you have access to criteria query methods such as eq(), or in this case, le(). Those methods take the name of the property as an argument, which is what you need. I tweaked the code a bit because you're actually interacting with domain classes, not tables. Small distinction, until you start working with HQL.
def cleanDomainClass(String domainClassName, String dateField) {
def domainClass = Class.forName("some.package.$domainClassName")
def comparisonDate = use (TimeCategory) { new Date() -1.year }
def numOfRecordsDeleted = domainClass.where { le(dateField, comparisonDate) }.deleteAll()
log.info("deleted : $numOfRecordsDeleted")
}
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)
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 domain classes A and B as follows:
class A {
String prop1
String prop2
B prop3
static embedded = ['prop3']
}
class B {
String prop4
String prop5
}
When I want to query like this:
def q = A.where { prop3.prop4 == 'bla' }
def list = q.list()
I get the following exception:
Cannot get property 'javaClass' on null object. Stacktrace follows:
on the "def q = A.where ..." line.
Any clue what's the problem? I've checked this:
http://grails.1312388.n4.nabble.com/GORM-embedded-object-issue-td1379137.html
but how to "just call them directly" is not quite clear to me. Any other way of querying the embedded objects in GORM?
I finally gave up on the where query and went with the DetachedCriteria approach. Gives me the same flexibility as the where queries, but works with embedded domain objects:
def criteria = new DetachedCriteria(A).build {
eq 'prop1', 'bla2'
}
criteria = criteria.build {
eq 'prop3.prop4', 'bla'
}
def list = criteria.list()
What do you get if you do (assuming B is in src/groovy)
def q = A.where { prop3 == new B(prop4: 'bla') }
def list = q.list()
Embedded components are persisted inside the main domain class (owner) itself. It can be accessed directly using any dynamic finder as you do directly on a domain object.
The above can also be represented in dynamic finders as:
A.findAllByProp3(new B(prop4: 'bla'))
I have the folowing:
class Store{
String name
}
class Shop{
String name
Store store
}
My criteria builder:
def c = Shop.createCriteria()
def results = c.list {
like("name", "Harrods")
like("store.name", "McDonals")
}
I'm sure this is invalid cause i'v tested it. How can i manage to use criteriaBuilder and do this: like("store.name", "McDonals")?
Looking forward to get any help,
John
Since you're querying an association, try:
def results = c.list {
like('name', 'Harrods')
store {
like('name', 'McDonals')
}
}
This will do an conjoined query between name and store.name.
Check out the documentation Looks like you need to use a % for your like clause.