无法通过休眠更新记录

我能够通过Hibernate成功在表中创建和插入条目,但是由于某种原因,我的更新方法似乎无法正常工作。


对于我的表,我选择使用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();

   }

}


守着星空守着你
浏览 122回答 2
2回答

胡说叔叔

您没有在此处发布错误,但是看起来您的Studentbean类没有默认的构造函数,该构造函数在执行时被调用Student studentToUpdate = (Student)newSession.get(Student.class, id);您可以在将默认构造函数与自定义构造函数一起添加到Student类之后尝试。

GCT1015

我的删除方法存在问题:public static void deleteStudent() throws HibernateException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session newSession = factory.openSession();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newSession.beginTransaction();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newSession.createQuery("delete from student s where smarks < 35")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .executeUpdate(); //Used for updates and deletes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newSession.getTransaction().commit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newSession.close();}如果仔细查看该查询,则“从学生中删除”应该是用大写字母s的“从学生中删除”。粗心的错误。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java