Grails

One-to-many - 'belongsTo' incompatible with 'joinTable'

Details

  • Type: Sub-task Sub-task
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.0.2
  • Fix Version/s: 1.1-beta3
  • Component/s: Persistence
  • Labels:
    None

Description

If I have the following two classes:

class Employee {
static hasMany = [projects: Project]
static mapping = { projects joinTable: [name: 'EMP_PROJ', column: 'PROJECT_ID', key: 'EMPLOYEE_ID'] }
}

class Project {
static belongsTo = Employee
}

I expect three tables (EMPLOYEE, PROJECT, EMP_PROJ), but instead of EMP_PROJ I get EMPLOYEE_PROJECT. It ignores the joinTable:name parameter (but not the column or key parameters). The reason is the belongsTo clause in Project. If I remove the belongsTo then I get EMP_PROJ as I wanted, but now Employee.delete() doesn't cascade to the related Projects. The workaround is to explicitly define the cascade behavior using something like:

projects joinTable: [name: 'EMP_PROJ', column: 'PROJECT_ID', key: 'EMPLOYEE_ID'], cascade: 'all,delete-orphan'

Activity

Hide
Jens Lukowski added a comment -

If you use cascade as a workaround for belongsTo, the validation does not cascade.

Show
Jens Lukowski added a comment - If you use cascade as a workaround for belongsTo, the validation does not cascade.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: