Sunday, March 23, 2008

Composite Id and FK as PK in GORM

I've been discussing with my colleage about how to map 2 classes in GORM. It's a bit complicated, where we need to use the foreign key from one, Process, class to be the primary key for another class, State.

Code listing as follows:

class Process implements Serializable {

String name
static hasMany = [states: State]

}



class State {

String stateId
Process process

static mapping = {
version false
id composite:['stateId','process'], generator:'assigned'
}

}



Don't forget to use obj.save(insert:true), when you have a GORM mapping with version=false and generation='assigned'.

You see, life is much more easier with GORM.

1 comment:

Anonymous said...

Thank you. It helped a lot.

I have a situation where I need to part of the composite id is itself a composite id. How do I handle that. For ex:
class Process implements Serializable {
String/long processId
String name
ParentOfProcess parentOfProcess
static hasMany = [states: State]}
id composite:['processId','parentOfProcess'], generator:'assigned'
}

How will I define the composite id for the state here?