1

In my Grails project I'm connecting to an existing DB and I'm having the following structure

class Country {
  Integer country
  String countryCode
  String label
}

class Address {
  String countryCode
  ....
}

DB tables:

country table

id, version, country, country_code, label

address table

id, version, country_code, ...

I would like to have something like:

class Address {
  Country country
}

But it seems it is automatically looking for a column called country_id in the address table, I've tried using

static mapping = {
    country column: 'country'
}

But I'm still having the same result.

I know it would better to link it to the country.id but the DB is existing and I cannot modify it.

Anybody can help on this?

1
  • So is there a column country in your existing db table address? Commented Feb 9, 2016 at 17:50

2 Answers 2

1

If you use one-to-many relationship, you do not need the property declared. Under the class Country you can add

static hasMany = [addresses: Address]

And add

static belongsTo = [country: Country]

You don't need to add Country country field.

Then you can use addressInstance.country.countryCode in your view to display the data.

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried it but I'm receiving this error: ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: column this_.country_id does not exist
I just reread your post and you cannot change the table. Did you try mapping static mapping = { country column: 'country_code' }
0

Check out this and add to domain class Country:

static mapping = {
     id name: 'country'
}

1 Comment

I've tried it but I'm receiving this error: ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: column this_.country_id does not exist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.