puneet « Intelligrape Groovy & Grails Blogs
Subscribe via E-Mail:

puneet

Posts by puneet:

  • Using aliases in aggregate functions of criteria queries

    13 Feb 2012 in Grails

    When using criteria queries, many times we encounter a use case where we need to order our result not on any field of domain class but on our result set, in these cases aliases can be used.

    
    

    In my project, use case was to find most liked products from the domain:

    ProductStat{
    Date dateCreated
    Product product
    }
    

    To find the result, the query I used was :

    ProductStat.createCriteria().list() {
                projections {
                    groupProperty("product")
                    count("product", "totalProducts")  // alias totalProducts
                }
                order("totalProducts", "desc")
    }
    
    
    

    In this way using alias “totalProducts” I was able to order the result according to the count of products.

    
    
    • Share/Bookmark
  • Using transactional behaviour with Gorm

    14 Mar 2011 in Grails

    All communication between Hibernate and the database runs within the context of a database transaction but the Session itself is lazy in that it only ever initiates a database transaction at the last possible moment.Given that there is a transaction, you would think that if something went wrong, any problems would be rolled back. However, without specific transaction boundaries and if the Session is flushed, any changes are permanently committed to the database.
    This is a particular problem if the flush is beyond your control for instance, the result of a query. Then those changes will be permanently persisted to the database. This is a particular problem if the flush is beyond your control for instance, the result of a query. Then those changes will be permanently persisted to the database which may create problems.

    For example

    def save = {
    def album = Album.get(params.id)
    album.title = "Changed Title"
    album.save(flush:true)
    ...
    // something goes wrong
    throw new Exception("Oh, sugar.")
    }
    

    Now when some exception occurs, the database has already been updated, you cannot roll back at this stage.This problem can be solved by either placing code in service or adding transactional behaviour to GORM

    def save = {
    Album.withTransaction {
    def album = Album.get(params.id)
    album.title = "Changed Title"
    album.save(flush:true)
    ...
    // something goes wrong
    throw new Exception("Oh, sugar.")
    }
    }
    

    If an exception is thrown, all changes made within the scope of the transaction will be rolled back as expected.

    Grails uses Spring’s PlatformTransactionManager abstraction layer under the covers. In this case, if an exception is thrown, all changes made within the scope of the transaction will be rolled back as expected. The first argument to the withTransaction method is a Spring TransactionStatus object, which also allows you to programmatically roll back the transaction by calling the setRollbackOnly() method.

    
    def save = {
    Album.withTransaction { status ->
    def album = Album.get(params.id)
    album.title = "Changed Title"
    album.save(flush:true)
    ...
    // something goes wrong
    if(hasSomethingGoneWrong()) {
    status.setRollbackOnly()
    }
    }
    }
    
    • Share/Bookmark