3

私は、fw1でcoldfusion9 ormを使用して、学生、学年、教師の関係を定義しようとしています。これが私が教師と生徒の関係に取り組んでいる方法です。モデルの Student.cfc

// 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";

先生.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";

グレード.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>

同様に grade メソッドを呼び出しています。

<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 エラー: 非 null プロパティが null または一時的な値を参照しています: Student.Teacher タイプ: アプリケーションの詳細: 根本原因:org.hibernate .PropertyValueException: 非 null プロパティが null または一時的な値を参照しています: Student.Teacher。

誰でも私を助けてください。ありがとう。

4

1 に答える 1

1

最初に教師エンティティを保存してから、それを生徒に追加します

于 2013-01-03T12:40:34.087 に答える