各位大侠好,小弟在编写一个小型的学生成绩管理系统,但一开始就遇到了麻烦,我是基于ssh进行开发的,我见立了一个记录各门功课的学生成绩,其表结构如下:
-------+---------+------+-----+---------+----------------+
 Field | Type    | Null | Key | Default | Extra          |
-------+---------+------+-----+---------+----------------+
 id    | int(11) | NO   | PRI | NULL    | auto_increment |
 grade | double  | YES  |     | NULL    |                |
 s_id  | int(11) | NO   | MUL | NULL    |                |
 c_id  | int(11) | NO   | MUL | NULL    |                |
-------+---------+------+-----+---------+----------------+
我插入了如下数据:
+----+-------+------+------+
| id | grade | s_id | c_id |
+----+-------+------+------+
|  1 |    89 |    1 |    1 |
|  2 |    81 |    1 |    2 |
|  3 |    94 |    1 |    3 |
+----+-------+------+------+(其中s_id 为学生表的主键,c_id为课程信息的主键)
当我想通过学生的用户名,来查询某个学生的所有课程成绩;代码如下:
public List<Grade> getGrades(String username) {
List<Grade> list = new ArrayList<Grade>();
s = this.getStudentInfo(username);
list = this.getSession().createSQLQuery("from Grade g where g.student.id=?")
                 .setLong(0, s.getId()).list();
return list;
}
但出现了如下问题:
Exception in thread "main" org.springframework.orm.hibernate3.HibernateSystemException: More than one row with the given identifier was found: 1, for class: com.feng.ssh_1.model.Grade; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: com.feng.ssh_1.model.Grade
  希望各位大侠能给我多提点建议,小弟谢谢啦!!!,如果有好的建议还可以加分哦!!!

解决方案 »

  1.   

    把代码贴全,顺便你debug下测下在那出的问题,
      

  2.   

    如果真是小型的系统感觉就没有必要用SSH了吧?这样做反而会小题大做了,相对来说写起来会繁锁些!
    用的什么数据库,开发环境?Struts,Hibernate和Spring的版本和配置信息给说下
      

  3.   

    用的是MySQL数据库,我想做一个学生成绩管理系统,
    import java.util.Set;/**
     *课程信息
     * @author feng
     *
     */
    public class Courses { private int id; private String name;   //课程名称

    private float  points;        //学分

    private int time;          //课时

    private Set<Teacher> teacher; private Set<Student> student;

    private Grade grade;  

    public Grade getGrade() {
    return grade;
    } public void setGrade(Grade grade) {
    this.grade = grade;
    } public int getTime() {
    return time;
    } public void setTime(int time) {
    this.time = time;
    } public Set<Teacher> getTeacher() {
    return teacher;
    } public void setTeacher(Set<Teacher> teacher) {
    this.teacher = teacher;
    } public Set<Student> getStudent() {
    return student;
    } public void setStudent(Set<Student> student) {
    this.student = student;
    } public int getId() {
    return id;
    } public void setId(int id) {
    this.id = id;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public float getPoints() {
    return points;
    } public void setPoints(float points) {
    this.points = points;
    }
    }
    学生类:import java.util.Date;
    import java.util.Set;/**
     * 学生基本信息
     * 
     * @author feng
     * 
     */
    public class Student { private int id; private String username; // 姓名 private String password; // 密码 private int age; // 年龄 private String number; // 学号 private Date date; // 入学时间

    private char sex;   //性别

    private String address;  //地址

    private String role;     //用户权限 private Classes classes;   //班級 private Accademy accademy;   // 学院 private Set<Courses> courses;   //课程 private Set<Teacher> teacher;    //老师

    private Grade grade;   //成绩

    public Grade getGrade() {
    return grade;
    } public void setGrade(Grade grade) {
    this.grade = grade;
    } public Set<Courses> getCourses() {
    return courses;
    } public void setCourses(Set<Courses> courses) {
    this.courses = courses;
    } public Set<Teacher> getTeacher() {
    return teacher;
    } public void setTeacher(Set<Teacher> teacher) {
    this.teacher = teacher;
    } public Classes getClasses() {
    return classes;
    } public void setClasses(Classes classes) {
    this.classes = classes;
    } public Accademy getAccademy() {
    return accademy;
    } public void setAccademy(Accademy accademy) {
    this.accademy = accademy;
    } public Date getDate() {
    return date;
    } public void setDate(Date date) {
    this.date = date;
    } public String getNumber() {
    return number;
    } public void setNumber(String number) {
    this.number = number;
    } public void setId(int id) {
    this.id = id;
    } public int getId() {
    return this.id;
    } public void setUsername(String username) {
    this.username = username;
    } public String getUsername() {
    return this.username;
    } public void setPassword(String password) {
    this.password = password;
    } public String getPassword() {
    return this.password;
    } public void setAge(int age) {
    this.age = age;
    } public int getAge() {
    return this.age;
    } public char getSex() {
    return sex;
    } public void setSex(char sex) {
    this.sex = sex;
    } public String getAddress() {
    return address;
    } public void setAddress(String address) {
    this.address = address;
    } public String getRole() {
    return role;
    } public void setRole(String role) {
    this.role = role;
    }
    }
    成绩类:
    public class Grade{

    private int id;
    private double grade;    //分数
    private Student student;  //学生
    private Courses courses;  //课程
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public void setGrade(double grade){
    this.grade=grade;
    }
    public Double getGrade(){
    return this.grade;
    }
    public void setStudent(Student student){
    this.student= student;
    }
    public Student getStudent(){
    return this.student;
    }
    public void setCourses(Courses courses){
    this.courses=courses;
    }
    public Courses getCourses(){
    return this.courses;
    }
    }
    插入的数据库的数据如下:insert into t_accademy values (1,'08','计算机科学与技术学院'),(2,'09','管理学院');
    insert into t_classes values (1,'计科07-3班',1,1),(2,'工商管理07-3',1,2);
    insert into t_courses values(1,'C++',6,96,1),(2,'管理学',2,32,2),(3,'数据结构',4,64,1);