kaleidos / grails-postgresql-extensions

Grails plugin to use postgresql native elements such as arrays, hstores,...
Apache License 2.0
78 stars 63 forks source link

Exception occurred: org.postgresql.util.PGobject cannot be cast to java.lang.String #97

Open sjjsvks opened 7 years ago

sjjsvks commented 7 years ago

Domain Class (Product.groovy) :

import grails.converters.JSON
import grails.converters.XML
import net.kaleidos.hibernate.postgresql.hstore.Hstore
import net.kaleidos.hibernate.usertype.JsonbMapType
import net.kaleidos.hibernate.usertype.*

class Product {

Long id    
@Hstore Map attrib
Map config

static mapping = {
    datasource 'ALL'
    table name: "product"
    id name: "id", column: "id", generator:'identity', params:[sequence:'product_seq']
    version false
    attrib type: HstoreType
    config  type: JsonbMapType
    autoImport false
}

static constraints = {
    id nullable: true        
    attrib nullable: true
    config nullable:true
}

static {
    JSON.registerObjectMarshaller(Product) { Product product ->
        def map = [
            id: product.id,
            config:product.config
        ]
        if(product.attrib) {
            product.attrib.keySet().each {
                map[it] = product.attrib[it]
            }
        }
        return map
    }
}

Service Class (ProductService.groovy) :

import grails.converters.JSON
import org.springframework.transaction.annotation.Transactional
import javax.servlet.http.HttpSession
import org.springframework.web.context.request.RequestContextHolder

@Transactional(rollbackFor=[Exception])
class ProductService {

@Transactional
def create(def product) {
    ---- Some Code ----
}

@Transactional(readOnly = true)
def update(product) {
    ---- Some Code ----
}

@Transactional(readOnly = true)
def delete(def product) {
    ---- Some Code ----
}

@Transactional(readOnly = true)
def list(id) {      
    def products;
    try{
println "Inside list service - Before getting data"  
            products = Product.findById("10")
println "Inside list service - After getting data"  
        }catch(Exception e){
            println "****\* ProductService.groovy : list : Error : "+e.getMessage()+"----------- " +e
        }  
        return products
    }   

}

When I try to get data from Domain class via Service class using GORM, following error will occur :

> Exception occurred: org.postgresql.util.PGobject cannot be cast to java.lang.String
> Message: org.postgresql.util.PGobject cannot be cast to java.lang.String
>     Line | Method
> ->>   49 | nullSafeGet        in net.kaleidos.hibernate.usertype.JsonMapType
> ---
> 
> |    105 | methodMissing      in org.grails.datastore.gorm.GormStaticApi
> |     84 | list . . . . . . . in com.app.admin.svc.ProductService

Note : 
Using postgres : 9.5.4
Using grails : 2.4.4
Using postgresql-extensions: 4.5.0
ilopmar commented 7 years ago

As you're using a Grails version with the patch applied you should map your attrib field to HstoreMapType instead of HstoreType and remove the @Hstore annotation on the field.

More info: https://github.com/kaleidos/grails-postgresql-extensions/tree/grails-2.x#grails-225-and-231