Hibernate 3.0的批量删除

Hibernate 3.0的批量删除

今天把孙MM的<<精通Hibernate>>里第五章的代码跑了一遍,有一个方法怎么也过不去:
  public void deleteAllObjects(String className) throws Exception{
    // Ask for a session using the JDBC information we've configured
    Session session = sessionFactory.openSession();
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      session.delete("from " +className);
      // We're done; make our changes permanent
      tx.commit();

    }catch (Exception e) {
      if (tx != null) {
        // Something went wrong; discard all partial changes
        tx.rollback();
      }
      throw e;
    } finally {
      // No matter what, close the session
      session.close();
    }
  }
总报错:
org.hibernate.MappingException: Unknown entity: java.lang.String
就是
 session.delete("from " +className);
这一句出错了.真是神奇,这么简单一段竟然过不去,立马写了一个测试,就是先Load一个NativeTester对象,然后再delete,成功了.......save一个NativeTester对象,也成功........这表示可以找到NativeTester.但是为什么说Unknown entity: java.lang.String 呢?
郁闷了一个下午以后,晚上开始google,终于让我解决了,原来孙MM写书的时候用的是2.0,现在我用的是3.0,批量删除的方法也已经改了,现在是:
String hqlDelete = "delete NativeTester"; 
int deletedEntities = session.createQuery( hqlDelete ) 
.executeUpdate();
终于解决了!!!!
补充一下,我的开发环境:
Eclipse 3.1.2+Myeclipse 4.1.1+Tomcat 5.0.28+Mysql 5.0.16

你可能感兴趣的:(Hibernate 3.0的批量删除)