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