I'm new to using the GRAG tool for generating Domain classes for GRAILS use, so far I've had to edit the domains pretty heavily in order to get them to work with a many to many relationship, and now everything works except I can't seem to add any new relations in the many to many relationship via Groovy.
I'm basically making this program to learn how to use GRAG as I'll be using it on a much larger database in the future with tons of relationships. I've been cracking away at this fo开发者_运维问答r a while and have had no success. So I come to you.
A quick description of the program:
Domains:- Person
- Project
- Task
Relationships:
Person:
- hasMany Tasks
- hasMany Projects
Project:
- belongsTo Person (needed for the many-to-many for some reason, I don't quite understand)
- hasMany Person
Task:
- belongsTo Person
My problem is I want to assign People to Projects and everything works right up until I call save() on the Project model.
Heres the addworker action in the ProjectController:
def addworker = {
def projectInstance = Project.get(params.id)
def workerInstance = Person.get(params.wid)
if(projectInstance){
if(workerInstance){
workerInstance.save()
projectInstance.workers.add(workerInstance)
if(!projectInstance.save(flush:true)){
flash.message ="WORKER ADDITION FAILED FOR WORKER ${workerInstance}"
}else{
flash.message ="Worker - ${workerInstance} - added."
}
redirect(action:"show", id:projectInstance.id)
}else{
flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'person.label', default: 'Person'), params.wid])}"
redirect(action: "list")
}
}else{
flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'project.label', default: 'Project'), params.id])}"
redirect(action: "list")
}
}
When I run the action I end up with my flash message saying it "Worker - 1:TestWorker - added" which is a worker that already exists in the database, and when debugging, the workerInstance and projectInstance both have values as I would expect, and the projectInstance's workers even adds the workerInstance before the save.
for whatever reason the save isn't saving the list of workers.
Here is the Project domain:
class Project {
static mapping = {
table 'project'
// version is set to false, because this isn't available by default for legacy databases
version false
columns{
id column:'pro_id'
proName column:'pro_name'
proDesc column:'pro_desc'
workers joinTable:[name:'assignment',key:'pro_id',column:'p_id'],lazy:false
}
}
String proName
String proDesc
// Relation
static belongsTo = [Person]
static def hasMany = [workers: Person]
static constraints = {
proName(size: 1..45, blank: false)
proDesc(size: 0..255)
workers()
}
String toString() {
return "${id}: ${proName}"
}
}
Although it may not be obvious I'm using a relational table, all it's entries hold are the id of a person and id of a project. Is there something special I have to do to add the new key sets to the table? I figured the mapping would take care of this.
I'm running a mysql database and the relational table is named 'assignments' If you need more information please let me know. Thank you!
I figured out my problem, I was trying to do:
projectInstance.workers.add(workerInstance)
when I should have been doing
projectInstance.addToWorkers(workerInstance)
I wasn't aware groovy was picky about this, but I suppose it makes sense, I'm guessing the addToXXX method converts the Domain into a usable/saveable form instead of an Object.
精彩评论