Grails Join Table column name - grails

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"]

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

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

Saving Nested Domain Objects in Grails

I UPDATE THE ISSUE BELOW
I am trying to save a series of nested Domain Objects in Grails and I running into issues with the syntax for this.
As an example, lets say I have the following nested one-to-many relationships:
Artist->Album->Song
I first create an Artist
Artist artist = new Artist(parameters)
artist.save()
With the newly saved Artist I wish to then save a series of Albums and their associated Songs.
I thought something like this would work but I am getting mixed results.
Album album = new Album()
artist.addToAlbum(album)
Song song = new Song()
album.addToSong(song)
What I see is a new Album domain object, but no new Song. I tried a simple println to check the status of the "album" and it returns a null. So clearly I can add an Album to the existing Artist, but I am not getting back the Album itself so that I can add Songs. Can someone shed some light on how to save multiple nested objects in GORM/Grails?
Thank you
Actual Domains: Nested DataLoad->DataSeries->ReportedResult
DataLoad
class DataLoad {
static hasMany = [dataSeries: DataSeries]
String fileLocation
Date dateCreated
Date lastUpdated
CROSource croSource
List dataSeries = new ArrayList()
static mapping = {
dataSeries cascade:"all-delete-orphan"
}}
DataSeries
class DataSeries {
//parent and child relationships one-to-many
static belongsTo = [dataLoad: DataLoad]
static hasMany = [reportedResult: ReportedResult]
//this functions as an experiment_id for data which needs to be viewed together
String randomString
Date dateCreated
Date lastUpdated
List reportedResult = new ArrayList()
static mapping = {
reportedResult cascade:"all-delete-orphan"
}}
ReportedResult
class ReportedResult implements Serializable {
//parent and child relationships one-to-many
static belongsTo = [dataSeries: DataSeries]
static hasMany = [resultDefData: ResultDefData]
List resultDefData = new ArrayList()
//has one-to-one relationship -may need to make this a one sided relationship does not always need a Curve
Curve curve
AssayGroup assayGroup
AssayDefinition assayDefinition
AssayProtocol assayProtocol
ResultStatistic resultStatistic
ResultType resultType
//actual result fields
Float resultValue
String resultValueUnit
String resultModifier
Boolean resultObtained
Date resultTime
Integer compoundID
Float resultStatValue
String resultComment
Float resultRawValue
String linkToExternalFile
String studyNumber
Date dateCreated
Date lastUpdated
static mapping = {
resultDefData cascade:"all-delete-orphan"
}}
UPDATE
So if I try something like the following, it works. Must be something else in one of my domains. I will try to fix my issue, but for reference this should work.
Artist artist = new Artist(artistName: "MyName")
artist.save()
Album album = new Album(albumName: "myAlbum")
artist.addToAlbums(album)
Song song = new Song(songName: "just a song")
album.addToSongs(song)

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

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