请教一个关于struts2中更新外键值的问题

yujie1985 2010-05-19 03:16:38
我的一个表叫score,里面的属性为course_no(这个是外键,我的course表中的一个项),student_no(这个也是外键,我的student表中的一个项)。我现在想往这个score表中插入数据:
Course course = new Course();
course = courseService.findByCourseNo(courseNo);
Student student = new Student();
student = studentService.findByStudentId(studentId).get(0);
score.setCourse(course);
score.setStudent(student);
courseinfoService.add(score);
这是我在函数中写的,但是出现错我,说是空指针异常!
我把score.hbm.xml贴上

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="grade.entity.Score" table="score">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<many-to-one name="student" class="baseInfo.entity.Student" fetch="select">
<column name="student_no" not-null="true" />
</many-to-one>
<many-to-one name="course" class="baseInfo.entity.Course" fetch="select">
<column name="course_no" not-null="true" />
</many-to-one>
<property name="credit" type="java.lang.Float">
<column name="credit" precision="53" scale="0" />
</property>
<property name="studentScore" type="java.lang.Float">
<column name="student_score" precision="53" scale="0" not-null="true" />
</property>
<property name="scoreflag" type="java.lang.Integer">
<column name="scoreflag" not-null="true"/>
</property>
</class>
</hibernate-mapping>

希望大家多多帮忙,谢谢了.
我的分数不太多,我自己留个零头,整数给你们,谢谢!
...全文
41 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jink2005 2012-08-12
  • 打赏
  • 举报
回复
listCourse = courseService.findByCollegeId(student.getCollege().getId());//获得学生所在的学院所有的课程

这里student.getCollege().getId()你会得到空值吗?我遇到问题,通过外键对象再得id得不到……
yujie1985 2010-05-19
  • 打赏
  • 举报
回复
这是我的course表
3 id int 4 0
0 course_no varchar 50 0
0 course_name varchar 50 0
0 college_name int 4 0
0 ctype char 10 0
0 score float 8 0
0 estyle char 10 0
0 cyear varchar 50 0

这是我的score表
3 id int 4 0
0 course_no int 4 0
0 student_no int 4 0
0 credit float 8 1
0 student_score float 8 0
0 scoreflag int 4 0

这是我的student表
3 id int 4 0
0 student_id varchar 20 0
0 student_name varchar 50 0
0 student_sex char 2 1
0 born_date datetime 8 1
0 collegeName int 4 0
0 majorName int 4 0
0 tele_num varchar 20 1
0 address varchar 200 1
0 className int 4 0
0 age int 4 1
0 oldschool varchar 100 1
0 [position] varchar 20 1
0 status int 4 1
0 comments varchar 400 1
yujie1985 2010-05-19
  • 打赏
  • 举报
回复
这个是我写的action
package student.action;

import grade.entity.Score;
import grade.service.CourseInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import login.entity.Login;
import baseInfo.entity.Course;
import baseInfo.entity.Student;
import baseInfo.service.CollegeService;
import baseInfo.service.CourseService;
import baseInfo.service.StudentService;

import com.opensymphony.xwork2.ActionContext;

public class SelectCourseAction {

private CourseInfoService courseinfoService;
private StudentService studentService;
private CollegeService collegeService;
private CourseService courseService;
private Student student;
private Score score;
private Course course;
private String courseNo;
private List<Score> listScore = new ArrayList<Score>();
private List<Course> listNoSelectedCourse = new ArrayList<Course>();
private List<Course> listCourse = new ArrayList<Course>();

public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public String getCourseNo() {
return courseNo;
}
public void setCourseNo(String courseNo) {
this.courseNo = courseNo;
}
public CollegeService getCollegeService() {
return collegeService;
}
public void setCollegeService(CollegeService collegeService) {
this.collegeService = collegeService;
}
public CourseService getCourseService() {
return courseService;
}
public void setCourseService(CourseService courseService) {
this.courseService = courseService;
}
public List<Course> getListNoSelectedCourse() {
return listNoSelectedCourse;
}
public void setListNoSelectedCourse(List<Course> listNoSelectedCourse) {
this.listNoSelectedCourse = listNoSelectedCourse;
}
public List<Course> getListCourse() {
return listCourse;
}
public void setListCourse(List<Course> listCourse) {
this.listCourse = listCourse;
}
public CourseInfoService getCourseinfoService() {
return courseinfoService;
}
public void setCourseinfoService(CourseInfoService courseinfoService) {
this.courseinfoService = courseinfoService;
}
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
public List<Score> getListScore() {
return listScore;
}
public void setListScore(List<Score> listScore) {
this.listScore = listScore;
}
@SuppressWarnings("unchecked")
public String getLogin()
{
ActionContext context = ActionContext.getContext();
Map session = context.getSession();
Login role = (Login)session.get("role");
return role.getUsername().trim();
}
public String queryAll()
{
String studentId = getLogin();
// System.out.println("登录名:++++++++++++++"+studentId);
student = studentService.findByStudentId(studentId).get(0);
listScore = courseinfoService.findByStudentId(student.getId());//学生已经选的课程
//System.out.println("listScore==========="+listScore);
//listScore可以实现queryAll页面的上半部分的功能
//System.out.println("student.getCollege().getId()=========="+student.getCollege().getId());
listCourse = courseService.findByCollegeId(student.getCollege().getId());//获得学生所在的学院所有的课程
//System.out.println("listCourse==========="+listCourse);
//下面的for循环是找出这个学院的这个学生没有选的课程
listNoSelectedCourse.clear();
for (Course c : listCourse)
{
int temp = 0;
for (Score s : listScore)
{
if (c.getCourseNo().equals(s.getCourse().getCourseNo()))
temp++;
}
//System.out.println("temp++++++++++"+temp);
if (temp == 0)
listNoSelectedCourse.add(c);//
//System.out.println("c============="+c.getCyear());
}
//System.out.println("listNoSelectedCourse======"+listNoSelectedCourse.size());
return "queryAll";
}
public String addSelect()
{
//获取courseNo和studentId,可以找到相应的表中的id,然后写到score表中,作为选课的一条记录
String studentId = getLogin();
System.out.println("studentId======"+studentId);
System.out.println("courseNo========="+courseNo);
Course course = new Course();
course = courseService.findByCourseNo(courseNo);
System.out.println("course++++++++"+course);
Student student = new Student();
student = studentService.findByStudentId(studentId).get(0);
System.out.println("student++++++++"+student);
score.setCourse(course);
score.setStudent(student);
courseinfoService.add(score);
return "tzqueryAll";
}
}
yujie1985 2010-05-19
  • 打赏
  • 举报
回复
package grade.entity;

import baseInfo.entity.Course;
import baseInfo.entity.Student;

/**
* Score entity.
*
* @author MyEclipse Persistence Tools
*/

public class Score{

// Fields

private Integer id;
private Student student;
private Course course;
private Float credit;
private Float studentScore;
private int scoreflag;


// Property accessors

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public Student getStudent() {
return this.student;
}

public void setStudent(Student student) {
this.student = student;
}

public Course getCourse() {
return this.course;
}

public void setCourse(Course course) {
this.course = course;
}

public Float getCredit() {
return this.credit;
}

public void setCredit(Float credit) {
this.credit = credit;
}

public Float getStudentScore() {
return this.studentScore;
}

public void setStudentScore(Float studentScore) {
this.studentScore = studentScore;
}

public int getScoreflag() {
return this.scoreflag;
}

public void setScoreflag(int scoreflag) {
this.scoreflag = scoreflag;
}

}
izard999 2010-05-19
  • 打赏
  • 举报
回复
你这是Hibernate, ..标题有误
cd0425 2010-05-19
  • 打赏
  • 举报
回复
代码 太少 看不出

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧