3

My Data Domain:

 class Course{
List teacherCourse
static hasMany = [courseByMultipleTeacher:TeacherCourse]
}

class Teacher{
    String name
    List teacherForCourses
    static hasMany = [teacherForCourses:TeacherCourse]
}

class TeacherCourse{
Course course
Teacher teacher
static mapping={
            id composite: ['teacher','course']
}

}

And in my controller:

save(){
Teacher teacherInst = new Teacher()
teacherInst.name = "T1"
teacherInst.save(flush:true)

Course c1 = Course.findById(3)
Course c2 = Course.findById(4)

def teacherCourseA = new TeacherCourse(teacher:teacherInst, course:c1)
def teacherCourseB = new TeacherCourse(teacher:teacherInst, course:c2)
teacherInst.addToTeacherCourse(teacherCourseA)
teacherInst.addToTeacherCourse(teacherCourseB)
teacherInst.save(flush:true)

//Output is: (unsaved) . Why?
println(Teacher.findById(teacherInst.id).teacherForCourses.get(0));

}

Output net.school.teacher.TeacherCourse : (unsaved)

The Teacher and TeacherCourse gets saved in database (PostgreSQL). I get the (unsaved) message when ever I try to retrieve the value from the list. I am not sure why is that. Any help would be appreciated. I looked up this link, but could not solve the problem.

Community
  • 1
  • 1
matuda
  • 195
  • 2
  • 16
  • I think `teacherInst` is modified after teacherInst.addToTeacherCourse(teacherCourseA) did you tried saving teacherInst again after modification. – turbo Feb 03 '14 at 02:27
  • I tried saving teacherInst.. still get the same error.. I will update the code as well.. – matuda Feb 03 '14 at 02:42
  • 1
    I didn't get why did you create the TeacherCourse domain. You could have directly added cource c1 to teacher instance. – turbo Feb 03 '14 at 02:52
  • Please add also method addToTeacherCourse on Teacher class to the code sample. – lukelazarovic Feb 03 '14 at 08:51
  • @turbo There is a many-many relationship between teacher and course... – matuda Feb 03 '14 at 14:53
  • @lukelazarovic I did not overload the addToTeacherCourse(). It is provided by framework for adding elements into the List.. – matuda Feb 03 '14 at 15:40
  • One reason why TeacherCourse instances are not saved could be that they don't pass validation (maybe because course is null?). You should try to save TeacherCourse instances separately and see if they are persisted.. – lukelazarovic Feb 03 '14 at 16:03
  • @lukelazarovic The data gets saved to database.. The problem is while retrieving it.. I will update the original post on this aswell.. Thank you.. – matuda Feb 03 '14 at 16:11

2 Answers2

1

I think a problem is with the id of TeacherCourse class - GORM (Hibernate) needs it's own single column id to handle instances in persistent context, making composite id is never good idea in ORM.

Let GORM have his id and just specify that course and teacher together are unique:

class TeacherCourse {
    Course course
    Teacher teacher

    static mapping = {
        course unique: 'teacher'
    }
}

Here you can check that net.school.teacher.TeacherCourse : (unsaved) is returned from toString() when id of the domain instance is null:

http://jira.grails.org/browse/GRAILS-8072

lukelazarovic
  • 1,510
  • 11
  • 19
  • TeacherCourse is a join table for many-many relationship.. It will have composite key... http://www.tomjewett.com/dbdesign/dbdesign.php?page=manymany.php and http://stackoverflow.com/questions/2238807/many-to-many-link-tables-in-grails-gorm-hibernate – matuda Feb 03 '14 at 16:46
  • You don't have many-to-many relationship between your domain objects. The way you made it it's two separate one-to-many relationships. If you would like a many-to-many relationship with a join table, look at the bottom of chapter 6.5.2.1 in Grails docs how to do it.. – lukelazarovic Feb 04 '14 at 09:00
0
save(){
 Teacher teacherInst = new Teacher()
 teacherInst.name = "T1"
 teacherInst.save(flush:true)

 Course c1 = Course.findById(3)
 Course c2 = Course.findById(4)

 def teacherCourseA = new TeacherCourse(teacher:teacherInst, course:c1).save(true)
 def teacherCourseB = new TeacherCourse(teacher:teacherInst, course:c2).save(true)

 //TRY!
 println(Teacher.findById(teacherInst.id).teacherForCourses.get(0));

}
Olencha
  • 418
  • 2
  • 11