I'm using a domain object to interface with a database in Grails.
When I use the list() method on a domain object to get all of the rows from a database it works great except for one thing. The object that comes back for each row also includes an attribute called "class". I've read some things about creating a custom marshaller that would allow me to remove that attribute from the object. Is that really the best way to not have to return the class attribute?
Thanks!
You can also use JSON.registerObjectMarshaller as below:
// BootStrap.groovy
JSON.registerObjectMarshaller( YourDomain ) {
it.properties.findAll { it.name != 'class' }
}
Refer here for a similar example.
Here's a link to change the way Grails renders JSON by default:
http://grails.org/doc/2.4.4/guide/single.html#defaultRenderers
Just change "NameOfDomainClass" to the class you want to render differently. In this case, the Domain Class.
import grails.rest.render.json.*
beans = {
bookRenderer(JsonRenderer, NameOfClass) {
excludes = ['class']
}
}
Related
Just got to know about the capability of createCriteria() method. Just wanna know that other than applying it on the Controller, is there a way to apply onto the domain classes as well? Probably on its own mapping to a property like:
static mapping = {
additionalInfo: Page.createCriteria().list()
}
Perhaps you may want to simply create a new field based the target field like the below example:
class myInfo {
String additionalInfo
String[] moreInfo // a transient field
getMoreInfo(){
def myresultmap = createCriteria.list{
// insert any other criteria shenanigans
}
return myresultmap
}
static transients = ['moreInfo']
}
In a controller return a view like normal with the Domain instance of class MyInfo
Then use in view like:
<g:each in="${domaininstancefromcontroller}">
${it.moreInfo[0]
</g:each>
see docs.
Hope this helps.
Just wanna know that other than applying it on the Controller, is
there a way to apply onto the domain classes as well?
Criteria queries are not limited to controllers, you can apply them elsewhere using the same syntax that you would in a controller. However, the particular example you show there is probably going to be problematic because you are trying to use GORM inside of the mapping block which is used to configure GORM.
Suppose I have Employee domain class, I want to create object of domain class from params map coming from UI side.
I can create object in two ways as follows
Normal way
Employee employee = new Employee(name: params.name, rollNo:
params.rollNo)
and so on. If domain class has 20 variables, then we need to write all variables in above constructor.
Following is best way to create object
Employee employee = new Employee(params)
Above constructor will populate object with matching params. Right.
Now my question comes here.
If suppose I have existing domain class object fetched from DB, Now I want to update this object from params map coming from UI.
What is best way to do this (like we do in above second option).
I think it is best to use command objects and bind it to the Employee.
here is sample pseudo code:
class EmployeeMgmtController {
def editEmp(EmployeeCmd cmd){
Employee editEmp = Employee.get(1)
editEmp.properties = cmd
editEmp.save()
}
}
class EmployeeCmd{
String id
static constraints = {
id blank:false,nullable:false
}
}
or,
you if your on controller, and still want to use params (and exclude any fields that you don't want to bind):
bindData(editEmp, params, [exclude:['firstName', 'lastName']])
If you want to achieve that in a service class, make your service implement grails.web.databinding.DataBinder then use the bindData method as demonstrated below.
import grails.web.databinding.DataBinder
class MyAwesomeService implements DataBinder {
/**
* Updates the given instance of a domain class to have attribute values specified
* in "newData" map.
*/
MyDomain updateMyDomainAttributes(MyDomain myDomianInstance, Map newData) {
bindData(myDomianInstance, newData)
myDomianInstance.save(flush: true)
}
}
People, I'm facing a problem with grails GORM, my Application is totally dependent of the DomainClass.list() method, it is in all of my create/edit GSPs, but now I need a particular behavior for listing objects. Being more specific I need to filter these lists (All of them) by one attribute.
The problem is I'm hoping not to change all the appearances of these methods calling, so is there a way to customize the behavior of the default list() method ? I need it to function just the way it does, but adding an ending filter.
Maybe you can use hibernate filter plugin (see here). This will allow you to filter all finder methods (including list()) based on a property:
static hibernateFilters = {
enabledFilter(condition: 'deleted=0', default: true)
}
Have you considered using names queries? You could always do something like this:
class DomainClass {
// ... class members
static namedQueries = {
myList { params->
// put your complicated logic here
}
}
}
Then you can just replace your calls to DomainClass.list() with DomainClass.myList.list().
with every project, I automatically run into a problem with reserved SQL word when I use properties like status or user in my Grails domain classes.
So I always have to add a
static mapping = {
status column:'prefix_status'
}
to my classes.
I now wonder if there is an easy way to prefix all columns with a given string?
If there is nothing out of the box, I guess it would be possible to create a plugin which automagically injects such a mapping in all domain classes - can someone point me to a code example which modifies a class whenever it changes?
This is already answered in the manual:
Object Relational Mapping (GORM) - Custom Naming Strategy
Add to DataSource.groovy Config:
hibernate {
...
naming_strategy = com.myco.myproj.CustomNamingStrategy
}
Custom Naming Class (under src/groovy/com/myco/myproj/CustomNamingStrategy.groovy):
package com.myco.myproj
import org.hibernate.cfg.ImprovedNamingStrategy
import org.hibernate.util.StringHelper
class CustomNamingStrategy extends ImprovedNamingStrategy {
String propertyToColumnName(String propertyName) {
"prefix_" + StringHelper.unqualify(propertyName)
}
}
Is it possible to metaprogram named queries onto a grails domain class? If so, how?
Thanks
Domain classes have a namedQueries property that you can use to add your own named queries. If you want to do this using metaprogramming from within a plugin (rather than by editing the domain class directly), you should do it in the doWithDynamicMethods closure of the plugin's descriptor file.
Something like this should work:
class MyPlugin {
def doWithDynamicMethods = { applicationContext ->
application.domainClasses.each { domainClass ->
boolean domainClassFilter = domainClass as Boolean
if (domainClassFilter) {
domainClass.metaClass.static.myNamedQuery = {->
// implementation of your named query goes here. Here is an example implementation
// that returns all instances with status == 'ready'
String simpleClassName = domainClass.simpleName
domainClass.findAll("from $simpleClassName where status = ?", ['ready'])
}
}
}
}
}
This will add myNamedQuery to every domain class in the application that the plugin is installed in. If you only want to add it to some domain classes, then replace the value of domainClassFilter with a more appropriate test.