当前位置: 代码迷 >> Eclipse >> 用的是Hibernate自动生成的delete语句,不知道调用该怎样写才能运行?该如何解决
  详细解决方案

用的是Hibernate自动生成的delete语句,不知道调用该怎样写才能运行?该如何解决

热度:31   发布时间:2016-04-23 13:40:30.0
用的是Hibernate自动生成的delete语句,不知道调用该怎样写才能运行?
DAO包中的: public void delete(Staff persistentInstanc) {
log.debug("deleting Staff instance");
Session session=null;
Transaction ts=null;
try {
session=getSession();
ts=session.beginTransaction();
  session.delete(persistentInstanc);
ts.commit();
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
测试程序:
  public class text4 {
public static void main(String args[]){
Staff staff=new Staff();
staff.setId(12);
  StaffDAO staffDAO=new StaffDAO();
  staffDAO.delete(staff);
}

}
运行老报错
Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: com.zzu.dao.Staff.name
at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
at org.hibernate.event.def.DefaultDeleteEventListener.deleteEntity(DefaultDeleteEventListener.java:250)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:141)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:52)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:766)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:744)
at com.zzu.dao.StaffDAO.delete(StaffDAO.java:57)
at com.zzu.text.text4.main(text4.java:12)
我不想使用自己新编的类来放入程序运行,想了解自动生成的数据库操作到底该如何运行,谢谢


------解决方案--------------------
你的straf状态不对,用session.delete( object) 其中object 必须得是持久状态

先用session.load 把straf 读取出来才可以用session.delete 删掉
------解决方案--------------------
写错了 是 staff

  相关解决方案