我写的 第一 
public void del1(int    aa)throws DataAccessException  {
session=getSession();
String sql = "delete Usertable aa where aa.userid=:userid";
  Query q = session.createQuery(sql);
  q.setInteger("userid", aa);
  int a=   q.executeUpdate();
System.out.println(a);
第二种:
public void del11(int    aa)throws DataAccessException  {
     session=getSession();
 try {
 tx=session.beginTransaction();
 Usertable ub1=new Usertable();
ub1.setUserid(aa);
session.delete(ub1);
tx.commit();
} catch (HibernateException e) {

e.printStackTrace();
if(tx!=null)
{
tx.rollback();
}
}
 finally{
 
 session.close();
 }
}
也没错 就是删除不了数据.为什么 头痛得很啊 

解决方案 »

  1.   

    delete Usertable aa where aa.userid=:userid"; 
    delete from Usertable aa where aa.userid=:userid"; 
      

  2.   

    1. 没有进行事务
    session=getSession(); 
    String sql = "delete Usertable aa where aa.userid=:userid"; 
      Query q = session.createQuery(sql); 
      q.setInteger("userid", aa); 
      int a=  q.executeUpdate(); 
    System.out.println(a); 
    2第二种没有多大问题
      

  3.   

    没有错误啊, 看看代码里有没有用log4j抛异常
    有的话加上log4j的JAR 包
    还加上一个 properties文件
      

  4.   

    据说hibernate先走缓存,与数据库同步需要提交事物或者session.flush();
      

  5.   

    一定要先seesion,flush(),让服务器同步,或者lazy=flase
      

  6.   

    一定会要先flush(),或者lazy属性设置为flase
      

  7.   

    代码没什么问题啊。你把hibernate的执行代码打印出来看看啊。 show_sql=true
      

  8.   

    第一种改成
    public void del1(int    aa)throws DataAccessException  { 
    session=getSession(); 
    String sql = "delete Usertable aa where aa.userid=:userid"; 
      Query q = session.createQuery(sql); 
      q.setInteger("userid", aa); 
      int a=  q.executeUpdate(); 
      session.commit();
    System.out.println(a); 第二种改成
    public void del11(int    aa)throws DataAccessException  { 
        session=getSession(); 
    try { 
    tx=session.beginTransaction(); 
    Usertable ub1=(Usertable )session.get(Usertable.class,aa); 
    session.delete(ub1); 
    tx.commit(); 
    } catch (HibernateException e) { e.printStackTrace(); 
    if(tx!=null) 

    tx.rollback(); 


    finally{ session.close(); 

      

  9.   


    一种改成
    public void del1(int    aa)throws DataAccessException  { 
    session=getSession(); 
    String sql = "delete Usertable aa where aa.userid=:userid"; 
      Query q = session.createQuery(sql); 
      q.setInteger("userid", aa); 
      int a=  q.executeUpdate(); 
     
    session.commit();

    System.out.println(a); 第二种改成
    public void del11(int    aa)throws DataAccessException  { 
        session=getSession(); 
    try { 
    tx=session.beginTransaction(); 

    Usertable ub1=(Usertable )session.get(Usertable.class,aa); 

    session.delete(ub1); 
    tx.commit(); 
    } catch (HibernateException e) { e.printStackTrace(); 
    if(tx!=null) 

    tx.rollback(); 


    finally{ session.close(); 


      

  10.   

    先看一下你的sql语句在数据库中是否可以执行!