16dots
16dots

Reputation: 3070

ID type mismatch when trying to get

Was just testing my code earlier this morning, and found something I cannot seems to resolve.

My SKU class has a custom ID generator(assinged) to take a String:

static mapping = {
    id generator: 'assigned', name: 'sku'
}

I created a SKU object with ID: "1234445" (Normally my SKU id is a mixture of dashes letters and numbers, but just for testing purposes I used a number as String)

Now whenever I try to do an SKU.get("1234445"), I get the following Error:

Provided id of the wrong type

Expected: class java.lang.String, got class java.lang.Long

Obviously I provided a String, somehow it's treating it as a Long when .get is performed, hence causing the error.

Any ideas on how to resolve this besides not using a String that looks like a number for SKU.id (Sku.sku in my case)?

Upvotes: 1

Views: 985

Answers (3)

Dana Leaman
Dana Leaman

Reputation: 125

Sorry for the thread necromancy, but I encountered the same problem recently. As it turns out (thanks to @Peter for the solution) you can workaround the issue by defining String id in addition to the String sku. The following is a little ugly but works:

class Sku {
    String id
    String sku
    static mapping = {
        id name: 'sku', generator: 'assigned'
    }
}

Then in a Spock test:

when:
(new Sku(sku: sku)).save()

then:
Sku.get(sku)

where:
sku << ['12345', 'f00-b4r']

Upvotes: 0

dmahapatro
dmahapatro

Reputation: 50245

Use String id instead of String sku, if want to use SKU.get("123445")

class SKU {
    String id
    static mapping = {
        id generator: 'assigned'
    }
}

def newSku = new SKU()
newSku.id = '123445'
newSku.save(flush: true)

println SKU.get("123445")

If you need to use sku specifically as the identifier then use

SKU.findBySku("123445")

with the mapping you have right now (as mentioned in the question).

Upvotes: 2

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

SKU.get("1234445" as String)

should work

Upvotes: 0

Related Questions