gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:06:29 25 4
gpt4 key购买 nike

我正在尝试使用带有 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)>

我在我的学生 Controller 中就是这个方法。这是我的老师.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>

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

谁能帮帮我。谢谢。

最佳答案

先保存teacher实体再添加到student

关于coldfusion - 无法将教师和成绩添加到学生记录中获取非空属性引用空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14137504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com