how to have arraylist in grails domain class - grails

I am new to grails. I have the following requirement, how to achieve it ?
class File {
list<Employee> listOfCaseWorkersWhoHaveWorkedOnThisFile; <<<---- how to achieve this ?
static constraints = {
}
}
class Employee {
list<File> filesOwnedByHim <<<<<-------- also this ?
static constraints = {
}
}
My requirement is that a File will have a list of Employees with zero or more elements and same for Employee.
Do I have to implement one of the GORM (one-to-one, one-to-many etc) here ? If yes how ?

You can use hasMany which is one-to-many association between your classes.
class File {
static hasMany = [employees: Employee]
static constraints = {
}
}
class Employee {
static hasMany = [files: File]
static constraints = {
}
}
Then you can easily use GORM operations on it (addTo,findBy,etc).
Ref: http://grails.org/doc/latest/ref/Domain%20Classes/hasMany.html

Related

How to set unique constraint in a hasMany relationship

I have this class:
class Word {
String word
static hasMany = [group: WordGroup]
static belongsTo = [language: Language]
static constraints = {
word(unique: ['language'], maxSize:255)
}
}
Which is working fine, but I would need to associate the unique constraint to both language and group, so different groups can insert the same "word".
The groups are in an intermediate table (WordGroup) as this is a many to many relationship.
If I try to do
word(unique: ['language', 'group'], maxSize:255)
I get the error
Missing IN or OUT parameter in index:: 3
Is there a way to do this?
EDIT: I am adding the other classes referenced in the Word class if it helps
WordGroup
class WordGroup {
static belongsTo = [word: Word, group: Group]
static constraints = {}
}
Group
class Group {
String name
String url
static hasMany = [words: Word]
static constraints = {
name blank: false, unique: true
}
}
Language
class Language {
String name
String code
static constraints = {
name maxSize:255
code maxSize:255
}
}
Thanks.

GORM One to Many relationship across multiple datasources

I see there is a strategy for a one to one mapping for domain objects across different databases. But I am trying to associate two Domain objects that are in different datasources and have a one to many relationship.
class DomainA {
// default data source
}
class DomainB {
static hasmany = [domainA: DomainA]
static mapping = {
datasource 'ds2'
}
}
Any suggestions on how to make this work? Or a workaround?
Found a solution to this and it works pretty well. Solution is to create a join table in the schema you own.
E.g.
class DomainA {
// default data source
}
class DomainB {
List<DomainA> domainAList
static transients = ['domainAList']
static hasmany = [domainAIds: Integer]
static mapping = {
datasource 'ds2'
domainAIds joinTable: [name: 'DOMAINB_DOMAINA', key: 'DOMAINB_ID', column: 'DOMAINA_ID']
}
List<DomainA> getDomainAList(){
domainAList = domainAIds.collect { DomainA.get(it) }
domainAList
}
}

How to specify table name of hasOne property in Grails

I use hasOne for one-to-one relation in Grails:
class MyParent {
static hasOne = [child: MyChild]
}
class MyChild {
static belongsTo = [parent: MyParent]
static mapping = {
table: 'MyChild'
}
}
I have table in the DB named "MyChild" and thus I get the next error:
Invalid object name 'my_child'
How can I specify relation's table name in Parent class to be "MyChild" and not "my_child"?
Try it without ':'.
static mapping = { table "mychild"}
or use name label
static mapping = { table name:"mychild" }
Hope this helps

Grails many to many using a third 'join' class

I read that a m:m relationship often means there is a third class that isn't yet required. So I have m:m on User and Project, and I created a third domain class, ProjectMembership
The three domains are as follows (minimized for illustration purposes):
User
class User {
String name
static hasMany = [projectMemberships : ProjectMembership]
}
Project Membership
class ProjectMembership {
static constraints = {
}
static belongsTo = [user:User, project:Project]
}
Project:
class Project {
String name
static hasMany = [projectMemberships : ProjectMembership]
static constraints = {
}
}
If I have the ID of the user, how can I get a list of Project objects that they are assigned to?
There are a handful of ways - here are a couple:
def user = User.get(userId)
ProjectMembership.findAllByUser(user).collect { it.project }
or to avoid the query for the User:
ProjectMembership.withCriteria {
user {
eq('id', userId)
}
}.collect { it.project }
Be wary of queries that'll return large result sets - you'll end up with a huge in-memory list of project objects.

How to Restrict hasMany with limited numbers in domain class in grails

I am developing a grails application, I have domain classes like Tip, TipTag. Concept is, For a Tip I should have only 5 TipTag.
class Tip {
String description
static hasMany = [tags:TipTag]
static constraints = {
description(nullable:false,size:50..500)
}
}
How to restrict this in grails??
Any other way of doing this???
The maxSize constraint should do the job for you:
class Tip {
String description
static hasMany = [tags:TipTag]
static constraints = {
description(nullable:false,size:50..500)
tags(maxSize: 5)
}
}

Resources