Reputation: 466
I have an Domain Class called Contact
with multiple hasMany Relationships and another Domain Class Employee
which is part of Concat
.
Contact
has an table contact
and Employee should be mapped on a View which looks like this:
SELECT * FROM contact where employee=1
Employee
should have the same columns and Relationship than Contact
, how do I write the Domain Classes?
Can I use inheritance?
EDIT
Now I have used inheritance like this:
class Employee extends Contact { }
class Contact{
static mapping = {
tablePerHierarchy(false)
}
}
That works so far, but now I want to add some Relationships to Employee
, like this:
class Employee extends Contact {
static belongsTo = [CostCenter ]
static hasMany = [costCenter: CostCenter]
static mapping = {
costCenter joinTable: 'employee_cost_center', column: 'employee_id'
}
}
class CostCenter {
static hasMany = [employees:Employee]
static mapping = {
employeesjoinTable: 'employee_cost_center', column: 'cost_center_id'
}
}
now I have the problem that the table 'employee_cost_center' makes an referen to Contact
which is good, but also added 'employee_id':
contact_id
employee_id
cost_center_id
So i could add the relationships to Contact but then I have in CostCenter
Contact
and not Employee
.
How can I add Relationships to Employee
?
Upvotes: 0
Views: 2434
Reputation: 9885
I think you're on track using inheritance. Since Employee
is backed by a database view which selects a subset of Contact
s, an Employee
is a Contact
. So you've got a good candidate for inheritance.
You described the employee
view as follows:
SELECT * FROM contact where employee=1
When using table-per-subclass inheritance the table generated for subclasses contain the following columns:
Since Employee
does not, and cannot add, additional properties, the view should only return the primary key.
SELECT id FROM contact where employee=1
I have an article that compares table-per-hierarchy to table-per-subclass inheritance and demonstrates what it looks like at the database level.
In your domain class examples you described a join table to create the many-to-many relationship between Employee
and CostCenter
. Join tables should have two, and only two, columns:
So your employee_cost_center
table should have the columns employee_id
and cost_center_id
. If you must specify the join table explicitly, use key
instead of column
.
costCenter joinTable: 'employee_cost_center', key: 'employee_id'
employees joinTable: 'employee_cost_center', key: 'cost_center_id'
You have Employee
belong to CostCenter
as so:
static belongsTo = [CostCenter ]
Maybe that's a typo, but if you're not defining a back-reference then the belongsTo should be defined as simply the class, like this:
static belongsTo = CostCenter
I've never used belongsTo this way so I don't know what it looks like in the database. But note that if you have a back-reference, defined like this:
static belongsTo = [costCenter: CostCenter]
Then, your employee
view must return a cost_center
column.
Upvotes: 1