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

Grails 3 GORM - must have same number of columns as the referenced primary key issue
“grails.mongodb.default.mapping” equivalent in application.yml in Grails 3.2
TenantId is null when saving an entity in a Discriminator based multi tenant application in Grails 3.2 with GORM 6
Grails 3 (unsaved) instance issue while fetching from database (GORM)
Can't use get() in domain using Grails3
GORM findAll + cannot pass dynamic List as named parameter
How to display database records in grails
Configure Grails 3.0.9 for Oracle 12c with identity PK?
Adding condition to findAll and findAllBy methods
Neo4j and Mongodb as datasource in Grails
GORM (varchar .save(), non DATE): ORA-01861: literal does not match format string
GORM addTo methods not found in Spock test
spring-boot property exception while externalizing datasource properties and using actuator
Mapping hasMany relation and saving the related data
Method on class [] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails Correctly
What happens when two app servers in cluster start LiquiBase update (via Grails)?

Categories

HOME
ms-access
ionic-framework
jdo
mockito
kde
tinymce
hashmap
webstorm
rsync
azure-storage-tables
basic
gorm
correlation
circuit
decomposition
reactcsstransitiongroup
solaris-10
django-cms
shared-hosting
jprofiler
immutable.js
css-animations
mustache.php
realex-payments-api
tibco-mdm
microsoft-sync-framework
buildbot
internet-explorer-9
tooltipster
nouislider
skia
bluestacks
directx-10
javascriptcore
ecto
google-cloud-endpoints-v2
code-contracts
normal-distribution
http-digest
scaffold
space-complexity
multilingual
espeak
unobtrusive-validation
mesos-chronos
android-tabhost
sfdc
core-plot
isbn
mime
bootstrap-dialog
reactive-cocoa-5
qcombobox
user-accounts
scrollable
osx-mavericks
linode
gnome-shell-extensions
google-cdn
color-picker
spring-android
home-directory
py2app
paypal-express
simplewebrtc
itextpdf
teamcity-8.0
google-places
eclipse-clp
ruby-2.2
feedback
tarjans-algorithm
varargs
sorl-thumbnail
java.util.concurrent
document-classification
poller
page-layout
ccss
odata4j
file-locking
eclipse-memory-analyzer
plasma
delphi-6
clipper
factory-method
anonymous-methods
rdoc
bluepill
manchester-syntax
nsmanagedobject
custom-backend
eclipse-templates
django-tagging
subviews
web-application-design
icanhaz.js
mozilla-prism
memory-size
eqatec
gacutil

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App