无法通过 Hibernate 更新记录

Unable to update a record via Hibernate

我能够通过 Hibernate 在 table 中成功创建和插入条目,但是由于某些原因,我的更新方法似乎不起作用。

对于我的table,我选择在POJO文件中使用Java注解来创建它。

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *
 * @author 
 */

@Entity
@Table(name="student") //name of DB table that will be created via Hibernate
public class Student {
   @Id //Primary Key
   @Column(name = "id") //map to column
   private Integer id;

   @Column(name = "name")
   private String name;

   @Column(name = "marks")
   private Integer marks;

   public Student(Integer id, String name, Integer marks) {
       this.id = id;
       this.name = name;
       this.marks = marks;
   }

   public Integer getId() {
       return id;
   }

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

    public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public Integer getMarks(){
       return marks;
   }

   public void setMarks(Integer marks) {
       this.marks = marks;
   }

   @Override
   public String toString() {
       return "Student: " + this.getId() + " | " + this.getName() + " | " + this.getMarks();
   }
}

如前所述,table 已在 MySQL 数据库中成功创建。但是,我无法通过我的 HQL 查询更新对象标记(等级):

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author 
 */
public class HibernateModuleTen {
    private static SessionFactory factory = new Configuration()
                                   .configure("hibernate.cfg.xml")
                                   .addAnnotatedClass(Student.class)
                                   .buildSessionFactory();
    public static void main(String[] args) {


        Session newSession = factory.getCurrentSession();

        try {

            /*Create Student Objects
              in Memory
            */
            Student student1 = new Student(100, "Greg", 95);
            Student student2 = new Student(101, "Mary", 91);
            Student student3 = new Student(102, "Sidi", 90);
            Student student4 = new Student(103, "Rokia", 92);
            Student student5 = new Student(104, "Abdel", 88);
            Student student6 = new Student(105, "Christine", 77);
            Student student7 = new Student(106, "Hamma", 90);
            Student student8 = new Student(107, "Ahmadu", 68);
            Student student9 = new Student(108, "Halimatu", 96);
            Student student10 = new Student(109, "Iziren", 99);

            //Begin transaction
            newSession.beginTransaction();
            //Save all the students
            newSession.save(student1);
            newSession.save(student2);
            newSession.save(student3);
            newSession.save(student4);
            newSession.save(student5);
            newSession.save(student6);
            newSession.save(student7);
            newSession.save(student8);
            newSession.save(student9);
            newSession.save(student10);
            newSession.getTransaction().commit();
            //Update a Student Record
            updateStudent(107, 34);

            //Delete a record if marks are less than 35 and then update Database
            deleteStudent();
            //Print all records
            newSession = factory.openSession();
            newSession.beginTransaction();
            Criteria newCriteria = newSession.createCriteria(Student.class);
            List<Student> students = newSession.createQuery("from Student").list(); //.list is .getResultList in later versions of Hibernate
            for (Student aStudent : students) {
                System.out.println(aStudent.toString());
            }
            newSession.getTransaction().commit();


        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            factory.close();
        }


    }
    public static void updateStudent(Integer id, Integer marks) throws HibernateException {
             /*Update Transaction*/
            Session newSession = factory.openSession();
            newSession.beginTransaction();
            Student studentToUpdate = (Student)newSession.get(Student.class, id); //Choose record 107 to update
            //Update the marks of Student based on ID and marks
            studentToUpdate.setMarks(marks);
            newSession.update(studentToUpdate);
            //Commit to the Transaction
            newSession.getTransaction().commit();
            newSession.close();
    }

    public static void deleteStudent() throws HibernateException {
            Session newSession = factory.openSession();
            newSession.beginTransaction();
            newSession.createQuery("delete from student s where smarks < 35")
                      .executeUpdate(); //Used for updates and deletes
            newSession.getTransaction().commit();
            newSession.close();
    }

}

update 方法有效地通过 id 列获取 table 中的记录之一,并更新 marks 列中的元素。

你在这里没有 post 你的错误,但看起来你的 Student bean class 没有在执行时调用的默认构造函数

Student studentToUpdate = (Student)newSession.get(Student.class, id);

您可以在将默认构造函数与自定义构造函数一起添加到 Student class 后尝试。

我的删除方法有问题:

public static void deleteStudent() throws HibernateException {
            Session newSession = factory.openSession();
            newSession.beginTransaction();
            newSession.createQuery("delete from student s where smarks < 35")
                      .executeUpdate(); //Used for updates and deletes
            newSession.getTransaction().commit();
            newSession.close();
}

如果仔细查看查询,"delete from student" 应该是 "delete from Student" 并且大写 s。粗心的错误。