Grails map domain class custom column name - grails

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?

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.

Check out this and add to domain class Country:
static mapping = {
id name: 'country'
}

Related

Grails/Gorm : Sorting hasMany relationship

I have a domain class that has a 'hasMany' relationship that I would like to sort on so results are consistent when retrieving. Below is an example of the domain class.
class Author {
static hasMany = [ books: Book ]
static mapping = {
books sort: 'title', order: 'asc'
}
}
This produces the following error.
Default sort for associations [Author->books] are not supported with
unidirectional one to many relationships.
How can I sort on title in this example?
I have been able to achieve sorting on another hasMany relationship. Any feedback would be most appreciated.
As the error suggests you need to make the relationship bidirectional for default order to work,
just add the following in Book domain
static belongsTo = [author:Author] if you need default sort order to work.
Check if it will work for you
class Author {
SortedSet books // add this to your Author domain
static hasMany = [books: Book]
}
class Book implements Comparable {
String title
int compareTo(obj) {
title.compareTo(obj.title)
}
}

Customizing the jointable column type of string lists in Grails domain classes

I've a domain class with a list of strings as one of many attributes.
Sometimes, the strings that are in the list have more than 255 chars. So,
how can I increase this limit in the database? Or change the column type for a CLOB or TEXT
type ??
If I understood you correctly, you want to change the type o the column in the join table of your domain class. You can do this in the mapping of your hasMany, through the type option.
class Person {
static hasMany = [nicknames: String]
static mapping = {
hasMany joinTable: [
name: 'bunch_o_nicknames',
key: 'person_id',
column: 'nickname',
type: "text"
]
}
}

Grails "duplicate field name" error in static mapping closure

I am receiving the following errors in my "Class" class in Grails. For each field, it is telling me that I have a duplicate field. Which doesn't make any sense, because all I'm trying to do is map the fields with their associated table columns. The class fields and the fields in my mapping closure are all underlined. Here is my class so far:
package booklist
class Class {
Integer id
String name
String description
String instructor
String courseNumber
String lineNumber
List books
BigDecimal bookTotalPrice
String sequenceNumber
String subjectCode
static constraints = {
}
static mapping = {
//Uses the default datasource
table ''
columns {
id column: 'class_id'
name column: 'class_name'
description column: 'course_description'
instructor column: 'instructor_name'
courseNumber column: 'course_number'
lineNumber column: 'line_number'
bookTotalPrice column: 'book_total_price'
sequenceNumber column: 'sequence_number'
subjectCode column: 'subject_code'
}
}
}
You don't need to declared in static mapping the fields that you don't need to rename. Just write this:
package booklist
class MyClass {
Integer id
String name
String description
String instructor
String courseNumber
String lineNumber
List books
BigDecimal bookTotalPrice
String sequenceNumber
String subjectCode
static mapping = {
description column: 'course_description'
}
}
Grails works with the CoC (Convention Over Configuration) approach: if you don't need to change something, don't write it and a convention will be used.
For more details about the column mapping, take a look at the Grails documentation: http://grails.org/doc/latest/ref/Database%20Mapping/column.html

Grails Scaffolding - define possible values for this property of a domain class

I am new to Grails. I have a Person domain class as :
class Person {
String firstName
String lastName
String gender
Date dateOfBirth
}
And wondering if I can define possible values for a property - say gender as {M, F, U} so that these three values will be listed in combo box when using dynamic scaffolding for Person controller.
Here I just wanted to know if there is such feature in Grails framework? If such feature exists , then how can I use it?
From the documentation http://grails.org/doc/latest/guide/scaffolding.html, you should be able to use an inList constraint:
class Person {
String firstName
String lastName
String gender
Date dateOfBirth
def constraints = {
gender( inList: ["M", "F", "U"])
}
}
This should scaffold to a select list for the gender field, depending on the version of Grails you're using. 2.0+ definitely does this.
Here is an alternative solution
class Person {
String firstName
String lastName
enum Gender {
M(1),
F(2),
U(3)
private Gender(int val) { this.id = val }
final int id
}
Gender gender = Gender.U
Date dateOfBirth
def constraints = {
gender()
}
}
This will store gender in the database as an integer (1,2,3) and default the gender to U. The benefit here is you can rename what F, M, and U mean without handling a data migration.

Many-to-Many of members of the same domain class

In Grails, I like to have a many-to-many relation among entries of the same domain class Person. Relations will link to different persons the "leftPerson" and the "rightPerson" since the "Parent-child" and "Employer-Employee" relations will discriminate the position of each link.
That I would like to have is something like the following model:
class Person {
String name
static hasMany = [relations:Relation]
}
class Relation{
String type
Person leftPerson
Person rightPerson
static belongsTo = [person:Person]
}
Any entry in Relations will be visible from both Persons.
I like to avoid having in Person two entries in'hasMany'and mappedBy if possible.
Is there any way to do it?
Take a look on many-to-many example of GORM many-to-many chapter.
class Person {
String name
static hasMany = [relations:Relation]
}
class Relation {
String type
static hasMany = [persons: Person]
static belongsTo = Person
}

Resources