register
other register

Tuesday, June 30, 2009

Digesting belongsTo


class Face {
String name
Nose nose
static constraints = {
nose (nullable: true)
}

String toString() {
return name
}
}

class Nose {
String name
static belongsTo = [face: Face]
String toString() {
return name
}
}

def a = new Face(name: 'joe')
def b = new Nose(name: 'blog')
a.b = b
a.save(flush: true)
b.save(flush: true)


// test
def face = Face.findWhere (name: 'joe')
println face.nose

def nose = Nose.findWhere(name: 'blog')
println nose.face

// You must set nullable: true for nose in the constraint, otherwise the save won't work and it doesn't throws any exception
face.nose = null
face.save()
println face



In the above relationship, the foreign key (i.e. nose_id) will added in the Face side. But you can still get hold of face from the nose, because nose belongsTo face.

Friday, June 26, 2009

No row with the given identifier exists

No row with the given identifier exists

class A {
B b
}

class B {
String name
}


If you have an A has B relationship. If from the database end, A says has B and with B's id, but actually that B's id doesn't exist in B's table. Then it will complains: No row with the given identifier exists

A workaround of this is to use ignoreNoFound like below

class A {
B b

static mapping = {
b ignoreNotfound : true
}
}