tiistai 23. marraskuuta 2010

Regular SQL query result mapped to Grails domain class

It's often needed to do a regular SQL query if Criteria or HQL query isn't flexible enough. This is achieved with Hibernate's org.hibernate.SessionFactory class function createSQLQuery. However query's list-method returns a List of objects inside of a List by default instead of List of domain instances. Luckily org.hibernate.SQLQuery class has a method named addEntity which can be used to map results to specific domain class.

Our domain class:
package fi.company.domain
class Product {
   String name
   int price
}


Service that uses sql
import org.hibernate.SessionFactory
import org.hibernate.Session
import org.hibernate.SQLQuery
class ProductSearchService {
   SessionFactory sessionFactory
   boolean transactional = false


   List<Product> searchProducts(Map params) {
      Session session = sessionFactory.getCurrentSession()
      SQLQuery sqlQuery = session.createSQLQuery("SELECT * FROM product WHERE ...")
      sqlQuery.addEntity(fi.company.domain.Product.class)
      return sqlQuery.list()
   }
}

maanantai 9. elokuuta 2010

Delete records with Hibernate Criteria Builder

A neat way of deleting records with createCriteria and Groovy:
def c = Agency.createCriteria().list {
eq("agency", "XXX")
}*.delete()
Source: http://stackoverflow.com/questions/2232621/how-do-you-bulk-delete-records-in-grails-gorm

tiistai 22. kesäkuuta 2010

Check if JSON object is undefined

on the callback of jQuery.getJSON(), you want to check if json.myObject is undefined. It's done in a groovy way:
function(json){
if (json.myObject){
// do stuff with myObject
} else {
// do not do stuff with myObject
}
}

Accessing Taglib from Service

From the service:


class MyService {
def grailsApplication

def method() {
def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
}
}

...now g can be used just as in GSP files.

Mystical message codes for numerical validation constraints

minSize -> class.prop.minSize.notmet
maxSize -> class.prop.maxSize.exceeded
size -> class.prop.size.toosmall & class.prop.size.toobig

min -> class.prop.min.notmet
max -> class.prop.max.exceeded
range -> class.prop.range.invalid (only if validated value is null), class.prop.size.toosmall , class.prop.size.toobig

perjantai 9. huhtikuuta 2010

Hibernate-plugin refuses to install: A workaround

Failed to install plugin [hibernate-1.2.2]. Plugin has missing JAR dependencies.

How to fix this? No clue, but a workaround is to clear the ~/.ivy2 directory.

tiistai 6. huhtikuuta 2010

Using session-scoped services in Grails' taglib

Using services with session scope in tag libraries does not work in Grails, as per version 1.2.2. There's a fix promised in 1.3.0, but while waiting for it to be released, here's a simple workaround:

import org.springframework.context.ApplicationContextAware
import org.springframework.context.ApplicationContext

class DefaultTagLib implements ApplicationContextAware {
ApplicationContext applicationContext

def someTag = {attrs, body ->
applicationContext.documentService.doSomething()
}
}