请教HQL语delete操作?
我用的是MySQL数据库
两个Pojo类Person PersonApply 我用的是Hibernate 它们是一对一唯一外键约束映射(fk:personID),现在我要做一个测试,就是通过删除personID来实现删除一条记录的功能 操作如下:
DAO层相关代码
public Person findByPersonID(long pid) {
// TODO Auto-generated method stub
Session s = HbnUtil.getCurrentSession();
Person person = null;
if(s != null)
{
Query q = s.createQuery("from Person p where p.personID=:pid");
q.setLong("pid",pid );
person = (Person)q.uniqueResult();
}
return person;
}
public Person deletePersonByID(long pid) {
Session s = HbnUtil.getCurrentSession();
Person person = null;
person = findByPersonID(pid);
if(s != null){
s.delete(person);
}
return person;
}
Service层相关代码
public Person deletePersonByID(long pid) {
// TODO Auto-generated method stub
Session s = null;
Transaction tran = null;
Person person = null;
try{
s = HbnUtil.getCurrentSession();
tran = s.beginTransaction();;
person = pdao.deletePersonByID(pid);
tran.commit();
}catch(HibernateException e){
if(tran == null)
tran.rollback();
e.printStackTrace();
}
return person;
}
测试类
这里数据已经全部添加进去,问题就出现在这里
PersonSerivceImpl phd = new PersonSerivceImpl();
phd.deletePersonByID(1L);
控制台出错信息为org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
找不出原因,请各位高手指点下,谢谢