gorm


How to avoid ImprovedNamingStrategy in joinTable in Grails


I have a legacy database which I can't change and I have this setup
class Foo {
static hasMany = [bars:Bar]
static mapping = {
version false
columns {
id column: "FooId"
color column: "FooColor"
bars joinTable: [name: "FooBar", key: 'FooId', column: 'BarId']
}
transient
def getBarName(){
((Bar)this.bars.toArray()[0]).name
}
}
class Bar {
static hasMany = [foos:Foo]
static belongsTo = [Foo, Baz]
static mapping = {
version false
columns {
id column: "BarId"
name column: "BarName"
}
}
When i try to access the method getBarName() in a controller Hibernate translates the inverse column name to "bar_id". Is there some way to set up a mapping like the one for the id and property columns?
And on a side note. How do i correctly implement getBarName()? Thacan't possibly be the correct implementation...
*EDIT*
----------------------------------------------------------------------
Apparently I was unclear above. The thing is that i already have a join column which has the form
-------------------
|RowId|FooId|BarId|
-------------------
| 1 | abc | 123 |
-------------------
Benoit's answer isn't really applicable in this situation since I want to avoid having a domain object for the joinTable.
*EDIT 2*
----------------------------------------------------------------------
Solved it. Dont understand it though... But split the join table information between the two domain classes and it works...
class Foo {
static hasMany = [bars:Bar]
static mapping = {
version false
columns {
id column: "FooId"
color column: "FooColor"
bars joinTable: [name: "FooBar", key: 'FooId']
}
transient
def getBarName(){
((Bar)this.bars.toArray()[0]).name
}
}
class Bar {
static hasMany = [foos:Foo]
static belongsTo = [Foo, Baz]
static mapping = {
version false
columns {
id column: "BarId"
name column: "BarName"
bars joinTable: [name: "FooBar", key: 'BarId']
}
}
As stated in the documentation Many-to-One/One-to-One Mappings and One-to-Many Mapping :
With a bidirectional one-to-many you can change the foreign key column
used by changing the column name on the many side of the association
as per the example in the previous section on one-to-one associations.
However, with unidirectional associations the foreign key needs to be
specified on the association itself.
Thye given example is:
class Person {
String firstName
static hasMany = [addresses: Address]
static mapping = {
table 'people'
firstName column: 'First_Name'
addresses column: 'Person_Address_Id'
}
}

Related Links

Script for saving large no of entries in mongodb
Does the multiple datasource feature of Grails 2.0 support relations?
How to avoid ImprovedNamingStrategy in joinTable in Grails
Grails 2 Show transient property that is an alias for id column
How to save GORM class with composite id made from its own field?
Grails - 2 domains and 1 form create/update/edit issues
Is there a belongsTo mixed syntax for GORM many-to-many and back-reference
Why do these Gradle tests throw exceptions?
In grails 2 when using multiple datasources, can I specify which datasource a criteria should use?
GORM methods are not accessible in Domain and Service classes
How to initialize the domain class properties with the values of another Domain class properties in grails
Grails Domain Embedded
How to get count of all items in a criteria GORM query
'Requested bean is currently in creation' on a domain object
GORM 1:N Association Cascades Delete Without belongsTo!
GNUstep - Can GORM open Interface Builder-created NIB files or not?

Categories

HOME
openstack
activiti
c#-4.0
heroku
pycharm
reserved
programming-languages
amazon-ecs
umd
mouse
cross-validation
virtualization
tomcat6
volttron
numeral.js
circular-dependency
hex-editors
iolanguage
lombok
tostring
pc
finite-automata
facebook-page
pugjs
reactive-cocoa
http-status-code-504
samoa
alpine
emulator
graphicsmagick
web-sql
sox
angular2-aot
qhull
sparse-matrix
facebook-apps
bpel
react-chartjs
ioio
titanium-mobile
google-sites-2016
http-get
gammu
webix-treetable
mesos-chronos
preconditions
angular-resource
pango
pubmed
fancybox-2
rdfs
sybase-asa
cudafy.net
vtigercrm
word-vba-mac
time-and-attendance
heidisql
infix-notation
radtreelist
elements
azure-virtual-network
clang-static-analyzer
revolution-r
execute
teamcity-8.0
collapse
metaclass
qdialog
tableau-online
sortedlist
lustre
c++03
pick
qpainter
fpml
internet-connection
typeof
java.nio.file
dexterity
spring-io
simba
browser-detection
dmoz
gil
genshi
infobox
coercion
gdlib
datareader
infrastructure
exchange-server-2003
procedural-music
windows-live-messenger
paul-graham

Resources

Encrypt Message