I have two tables, one of which (legacy table: A) has two fields that should serve as a composite foreign key and the other one (new table: B) should use a composite primary key
The B class has to be declared Serializable and implements the equals and hashCode methods
import org.apache.commons.lang.builder.HashCodeBuilder
class B implements Serializable{
static auditable = true;
String name;
String className;
String eventName;
boolean equals(other) {
if (!(other instanceof B)) {
return false
}
other.className == className && other.eventName == eventName
}
int hashCode() {
def builder = new HashCodeBuilder()
builder.append className
builder.append eventName
builder.toHashCode()
}
static mapping = {
//supposed to make a composite PK
id composite:["className", "eventName"]
}
}
and the A class just must to have an attribute of the B class and the GORM make the composite foreign key
class A {
//composite foreign key to link B class
B b; //instance of B to be related
static mapping = {
table 'a_table';
id column: 'id';
}
}
it create two tables in the database
+--------------------------------------------------+
| A_TABLE |
+--------+-----------+--------------+--------------+
| id | version | b_className | b_eventName |
+--------+-----------+--------------+--------------+
--where the Primary key is "id" and the foreign key are "b_className and b_eventName"
+--------------------------------------------------+
| B |
+--------+-----------+--------------+--------------+
| name | version | className | eventName |
+--------+-----------+--------------+--------------+
--where the Primary key are "className and eventName"
if you what to change the name of the columns to other just add the clausule in the mapping statement
class A {
//composite foreign key to link B class
B b; //instance of B to be related
static mapping = {
table 'a_table';
id column: 'id';
columns {
b {
column name: "className"
column name: "eventName"
}
}
}
}