Details
-
Type:
Bug
-
Status:
Open
-
Priority:
Major
-
Resolution: Unresolved
-
Affects Version/s: 1.0.2
-
Fix Version/s: 2.3-M2
-
Component/s: Persistence
-
Labels:None
-
Environment:normal grails development env, with postgres instead of hsqldb
Description
I have two domain classes, Task and User, that have many-to-many and one-to-many relations between them.
I cannot put the GORM mapping to work properly. The only way I could put it to work was with hibernate xml mappings.
I tried it in two ways: with and without mappedBy static attribute.
1) Without mappedBy.
class User{
String name
String email
static hasMany = [observedTasks: Task]
static mapping =
{ table 'appuser' observedTasks column:'user_id', joinTable:'user_task' }}
class Task {
String name
String code
User reporter
User assignee
static def hasMany = [observers: User]
static def belongsTo = [User]
static mapping =
{ observers column:'task_id', joinTable:'user_task' }}
The bug I identified was that GORM has generated the following join table:
CREATE TABLE user_task
(
user_id int8 NOT NULL,
assignee_id int8,
task_id int8 NOT NULL,
CONSTRAINT fkx FOREIGN KEY (assignee_id) REFERENCES task (id) ,
CONSTRAINT fk2 FOREIGN KEY (task_id) REFERENCES task (id),
CONSTRAINT fk1 FOREIGN KEY (user_id) REFERENCES appuser (id) )
)
The problem is that the column assignee_id should not exist (neither the FK associated with it)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
2) With mappedBy.
class User{
String name
String email
static hasMany = [observedTasks: Task, reportedTasks: Task, assignedTasks: Task]
static mappedBy = [reportedTasks:'reporter', assignedTasks:'assignee']
static mapping =
{ table 'appuser' observedTasks column:'user_id', joinTable:'user_task' }}
class Task {
String name
String code
User reporter
User assignee
static def hasMany = [observers: User]
static def belongsTo = [User]
static mapping =
{ observers column:'task_id', joinTable:'user_task' }}
What happens is that four tables are generated:
appuser
appuser_task (this is the one that is totally crazy. Should not exist at all)
task
user_task (this one is created with a assignee_id column that should not exist)
Am I doing something wrong with these two classes (Task and User)? Or is it a GORM bug?
Tks
Felipe
Pushing back to 1.0.4. Please attach a sample project that reproduces the problem, if you can.