我能够通过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 Hibernatepublic 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(); }}
2 回答
![?](http://img1.sycdn.imooc.com/54584f9d0001219b02200220-100-100.jpg)
胡说叔叔
TA贡献1804条经验 获得超8个赞
您没有在此处发布错误,但是看起来您的Student
bean类没有默认的构造函数,该构造函数在执行时被调用
Student studentToUpdate = (Student)newSession.get(Student.class, id);
您可以在将默认构造函数与自定义构造函数一起添加到Student类之后尝试。
![?](http://img1.sycdn.imooc.com/545846070001a15002200220-100-100.jpg)
GCT1015
TA贡献1827条经验 获得超4个赞
我的删除方法存在问题:
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();
}
如果仔细查看该查询,则“从学生中删除”应该是用大写字母s的“从学生中删除”。粗心的错误。
添加回答
举报
0/150
提交
取消