无法将教师和成绩添加到学生记录中,获取非null属性引用null

时间:2013-01-03 10:57:00

标签: coldfusion coldfusion-9

我正在尝试使用coldfusion9 orm和fw1定义学生,年级和老师之间的关系。 这就是我如何处理老师和学生之间的关系。 Model.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";

这是我保存学生记录并将教师和成绩设置为学生对象的方式。

<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)>

我是学生管理员中的这种方法。 这是我在服务中的teacher.cfc

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

就像我称之为成绩方法一样。

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

这是我在添加学生表格中选择教师姓名和成绩名称的方式。

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

我在将学生和成绩保存到学生记录时遇到了问题。这是我的错误 行动:student.save 错误:not-null属性引用null或transient值:Student.Teacher 类型:应用 详细信息:根本原因:org.hibernate.PropertyValueException:not-null属性引用null或transient值:Student.Teacher。

任何人都可以帮助我。 感谢。

1 个答案:

答案 0 :(得分:1)

首先保存教师实体,然后将其添加到学生