Grails change database column size of a hasMany of Strings - grails

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

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

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

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: How can I create named unique constraint for multiple columns?

How can I create named unique constraint for multiple columns?
I've three classes:
class Descriptor {
// some columns
}
class Protein {
// some columns
}
class DescriptorValue {
// some columns
static belongsTo = [protein: Protein, descriptor: Descriptor]
static constraints = {
protein(unique:['descriptor'])
}
}
GORM creates an index with an auto generated name that is different for different environments. How can I specify its name?
Try to do it like:
static mapping = {
protein unique:['descriptor'], index: 'protein_idx' //or whatever name you like
}
If you need to use multi-column indices, then you can specify for every property the same index name.
String field1
String field2
Integer field3
SomeObject object
static constraints = {
object unique: ['field1','field2', 'field3']
}

Grails Join Table column name

My Alert has many Location objects, and I have the join table alert_locations.
The generated columns are:
alerts_locations_id (I want this to be alert_id)
location_id
Here's my domain object:
class Alerts {
static hasMany = [locations:Locations,notifications:Notifications]
Date alertDateTime
String pest
String crop
static constraints = {
alertDateTime (blank:false)
pest (blank:false)
crop (blank:false)
}
}
static mapping = {
locations joinTable:[column:"location_id", key:"alert_id"]

Resources