Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sjjsvks opened this issue Oct 18, 2016 · 1 comment

Comments

@sjjsvks
Copy link

sjjsvks commented Oct 18, 2016

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
Copy link
Collaborator

ilopmar commented Oct 18, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants