In order to achieve a foreign key column (no joinTable) when mapping a unidirectional OneToMany relationship, i follow the steps according to Grails (version 2.2.3) reference documentation, section 6.5.2.1 OneToMany mapping, which states
With unidirectional associations the foreign key needs to be specified on the association itself. For example given a unidirectional OneToMany relationship between Person (replaced by One) and Address (replaced by Many), the following code will change the foreign key in the Many table
class One {
static hasMany = [manyCollection:Many]
static mapping = {
manyCollection(column:"ONE_ID")
}
}
However, when i run
One one = new One()
one.addToManyCollection(new Many())
one.save()
I get in the console
insert
into
one
(id, version)
values
(null, ?)
insert
into
many
(id, version)
values
(null, ?)
insert
into
one_many
(one_many_collection_id, many_id)
values
(?, ?)
Notice Grails create a joinTable called one_many. So, my question: it is a bug or something else ? What should i do to get rid of the joinTable ?
Even when i use something like
class One {
static hasMany = [manyCollection:Many]
static mapping = {
manyCollection(joinTable:false)
}
}
as highlighted here, i get the same output
You're almost there, you have 2 halves of the mapping but you need both for it to work correctly. It's not a well documented feature but I've used this before and it definitely works (e.g. no join table). So in your One class,
class One {
..
static mapping = {
manyCollection column: "ONE_ID", joinTable: false
}
}
NOTE: it's probably a good idea to exit, clean and restart grails when making mapping changes. I've lost many hours banging my head when a simple grails clean was the solution.
Also, you could technically add belongsTo
without the back reference in your Many class and you would still have a true uni-directional but with the added benefit of cascading action (mapped in One class) if it's something you need.