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

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?
How to Alphabetically retrieve members of a list?
Gorm main window does not accept drag-and-drop

Categories

HOME
amazon-ec2
azure-data-factory
hashmap
iot
google-docs
sharepoint-designer
indesign
medical
midi
izpack
offline
imacros
fatal-error
ab-testing
excel-vba-mac
google-cloud-speech
visual-composer
swiftlint
zapier
clickonce
crosstab
java-7
facebook-access-token
traffic
neo4j-spatial
stormpath
bcd
.net-4.0
xquery-3.0
azure-sql-database
y86
catalog
overriding
twitch
unobtrusive-validation
bootstrapper
cookiecutter-django
estimote
convertapi
multipeer-connectivity
nstextview
typed-lambda-calculus
httplistener
intrusion-detection
skobbler-maps
bstr
ubuntu-10.04
hexdump
itextpdf
phpcas
rtbkit
reactive-banana
sdhc
qdialog
spidermonkey
dukescript
sailfish-os
e10s
system.reflection
modalpopup
tween
xc16
tablelayout
aapt
rtmfp
picturefill
aqtime
spring-io
dbconnection
ftps
dataadapter
chronoforms
yui-compressor
jelly
delphi-6
selected
sublist
cascalog
chrono
postgresql-performance
transactionscope
fireworks
krl
winbugs14
ext3
multi-tier
thread-local-storage

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App