Grails/Gorm : Sorting hasMany relationship - grails

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)
}
}

Related

Grails map domain class custom column name

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'
}

Query domain class associations in Grails

I have a few domain classes similar to the following:
class Position {
String code
String title
static hasMany = [relations: Relation]
}
class Unit {
String code
String title
static hasMany = [relations: Relation]
}
class Relation {
Position position
Unit unit
static belongsTo = [
position: Position,
unit: Unit
]
}
I am attempting to use criteria to find all positions that do not have any relations. I know this can be solved using HQL but I find the criteria to be cleaner when building dynamic criteria versus building a dynamic HQL string.
Is there a way to use criteria to do something like:
Position.withCriteria { isNull('relations') }
I have tried the above but I always get a list containing 0 elements even though I know there are unrelated positions in the table.
For collections you need to use isEmpty() instead of isNull().
Position.withCriteria { isEmpty('relations') }

Grails change database column size of a hasMany of Strings

I have a domain class that looks like the following:
class Foo {
static hasMany = [bar: String]
}
The problem is that this creates a join table with a column of VARCHAR(255) in MySQL, which much larger than I need it to be. In my example, bar is a Set not an indexed collection, so trying to use indexColumn does not work. joinTable does not have an attribute to change the column type/length. Is it possible to change to column size without changing the structure of the domain class?
This works (tested with grails 2.4):
class Foo {
static hasMany = [
bars:String
]
static mapping = {
bars joinTable: [column: 'BARS_STRING', length: 112]
}
}
You can also try sqlType, like
class Foo {
static hasMany = [bar: String]
static mapping = {
names joinTable: [column: 'bar', sqlType: 'varchar(32)']
}
}

GORM, one class has both many-to-one and many-to-many (needs two belongsTo)

In my class I need to do something like below:
class Category {
static hasMany = [subCategories: Category, contracts: Contract]
static belongsTo = [parentCategory: Category]
}
Which mean I need two belongsTo, however for
static belongsTo = [parentCategory: Category]
, it's OK, but for Contract many-to-many mapping, I need to write something like this:
static belongsTo = Contract
there is some problem. How can I write them in the same line?
Can I write something like this:
static belongsTo = [parentCategory: Category, contract: Contract]
Thanks.

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