3
votes

I am trying to define relationship between student,grade and teacher using coldfusion9 orm with fw1. this is how i am working with the relationship between teacher and student. Student.cfc in Model

// Use a mysql autonumber for an ID
              property name="id" column="school_studentid" type="numeric" fieldtype="id" generator="identity";
              property name="Fullname" column="school_studentFullname" type="string" length="128" required="true" notnull="true";
              property name="email" column="school_studentEmail" type="string" length="128" required="true" notnull="true";
              property name="birthdate" column="school_studentbirthdate" type="date" required="false" notnull="true";
              property name="gender" column="school_studentgender" type="boolean" required="false" notnull="true";
              property name="phone" column="school_studentphone" type="string" required="false" notnull="true";
              property name="city" column="school_studentcity" type="string" required="false" notnull="true";
              property name="state" column="school_studentstate" type="string" required="false" notnull="true";
              property name="country" column="school_studentcountry" type="string" required="false" notnull="true";
              property name="subject" column="school_studentsubject" type="string" required="false" notnull="true";
              property name="Address" column="school_StudentAddress" ormtype="text";


              //relate student with teacher
              property name="Teacher" fieldtype="many-to-one" cfc="Teacher" fkcolumn="teacher_school_studentId" singularname="Teacher";

Teacher.cfc

 // Use a mysql autonumber for an ID
              property name="id" column="school_teacherid" fieldtype="id" generator="identity" generated="insert";
              property name="Fullname" column="school_teacherFullname" type="string" length="128" required="true" notnull="true";
              property name="email" column="school_teacherEmail" type="string" length="128" required="true" notnull="true";
              property name="birthdate" column="school_teacherbirthdate" type="date" required="false" notnull="true";
              property name="gender" column="school_teachergender" type="boolean" required="false" notnull="true";
              property name="phone" column="school_teacherphone" type="string" required="false" notnull="true";
              property name="city" column="school_teachercity" type="string" required="false" notnull="true";
              property name="state" column="school_teacherstate" type="string" required="false" notnull="true";
              property name="country" column="school_teachercountry" type="string" required="false" notnull="true";
              property name="Degree" column="school_teacherDegree" ormtype="text";
              property name="experience" column="school_teacherExperience" ormtype="text";
              property name="subject" column="school_teachersubject" ormtype="text";
              property name="Active" column="school_teacherActive" ormtype="text";
              property name="Address" column="school_teacherAddress" ormtype="text";

             property name="Student" fieldtype="one-to-many" cfc="Student" fkcolumn="teacher_school_studentId" singularname="Student" cascade="all" inverse="true";

grade.cfc

component output="false" persistent="true" accessors="true" entityname="Grade" table="school_grade" {


         //Use mysql autonumber for an Id
         property name="id" column="school_gradeId" fieldtype="id" generator="identity";
         property name="Name" column="school_gradeName" type="string" length="128" required="false" notnull="true";


         //Relate the Grade with student
         property name="Student" fieldtype="one-to-many" cfc="Student" fkcolumn="grade_school_StudentId" notnull="true" singularname="Student" lazy="extra";

this is the way i am saving the student record and setting teacher and grade to the student object.

<cfset var teacher = getTeacherService().teacher(arguments.rc.id)>
<cfset var grade = getGradeService().grade(arguments.rc.id)>
<cfset Student.setTeacher(teacher)>
<cfset Student.setGrade(grade)>
<cfset getStudentService().save(student)>

I am this method in my student controller. this is my teacher.cfc in services

<cffunction name="teacher" returntype="component" access="public">
              <cfargument name="id" required="true">

              <cfif Len(trim(arguments.id)) EQ 0 or arguments.id EQ 0>
                    <cfset result = entityNew("Teacher")>
              <cfelse>
                    <cfset result = entityLoad("Teacher",arguments.id,true)>
              </cfif>
              <cfreturn result>
        </cffunction>

In the same way i am calling grade method.

<cffunction name="grade" returntype="component" access="public">
                   <cfargument name="id" required="true">
                   <cfif Len(trim(arguments.id)) EQ 0 or arguments.id EQ 0>
                        <cfset result = entityNew("Grade")>
                  <cfelse>
                        <cfset result = entityLoad("Grade",arguments.id,true)>
                  </cfif>
                  <cfreturn result>

          </cffunction>

this is the way i am selecting teacher name as well as grade name in my add student form.

<select name="TeacherId" id="TeacherId">
                                <option value="0"<cfif rc.student.hasTeacher(teacher)>Selected</cfif>>Select Teacher</option>
                                <cfloop array="#rc.teacher#" index="teacher">
                                <option value="#teacher.getId()#"<cfif rc.student.hasTeacher(teacher)>Selected</cfif>>#teacher.getFullname()#</option>
                                </cfloop>
                        </select>

I am having issue while saving teacher and grade to the student record.this is my error Action: student.save Error: not-null property references a null or transient value: Student.Teacher Type: Application Details: Root cause :org.hibernate.PropertyValueException: not-null property references a null or transient value: Student.Teacher.

can anyone please help me out. thanks.

1
What does arguments.rc.id represent and why are you passing that same value ( arguments.rc.id) to both getTeacherService().teacher() and getGradeService().grade()?Scott Stroz
I am passing that values to be stored in student then i can access the teacher and grade base on their IDs. when i remove that lines i get this error. The value for property java.lang.String cannot be retrieved from object of type id. Expected object type is Gradesuleman khan
I am setting the teacher in student object then saving it.suleman khan
What do getTeacherService().teacher() and getGradeService().grade() do? It seems you are trying to load a Teacher and Grade object, respectively...however, you are using the same value to load both of them.Scott Stroz
because i want to add teacher as well as grade to the student record. I am using getTeacherService().teacher(arguments.rc.id) and getGradeService().grade(arguments.rc.id). to load the ID's of both object in the teacher and grade object respectively then setting it into Student object and saving it at last. so that it will stored in student fkcolumn.suleman khan

1 Answers

1
votes

Save the teacher entity first and then add it to the student