今天使用session.delete(sql);报下面的错误:
 
org.hibernate.MappingException: Unknown entity: java.lang.String
 at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:569)
 at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1086)
 at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:63)
 at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:579)
 
查查终于知道原因:
 
    session.delete(sql)是2.0的接口,3.1中已经废弃,同时废弃的有:
    find()、iterate()、filter()和delete(String hqlSelectQuery),saveOrUpdateCopy()
 
  如果要使用的话,可以采用以下方式创建Session实例: 

      org.hibernate.classic.Session session=sessionFactory.openSession(); 
 
      org.hibernate.classic.Session保留了2.0的接口
 
下面是3.1的reference里面这样写的,我使用了,但是还是报错,郁闷:
 
1) Hibernate3.0执行批量删除的程序代码: 
Session session = sessionFactory.openSession(); 
Transaction tx = session.beginTransaction(); 
String hqlDelete = "delete Customer where name = :oldName"; 
int deletedEntities = s.createQuery( hqlDelete ) 
.setString( "oldName", oldName ) 
.executeUpdate(); 
tx.commit(); 
session.close();
 
2)Hibernate3.0执行批量更新的程序代码: 
Session session = sessionFactory.openSession(); 
Transaction tx = session.beginTransaction(); 
String hqlUpdate = "update Customer set name = :newName where name = :oldName"; 
int updatedEntities = s.createQuery( hqlUpdate ) 
.setString( "newName", newName ) 
.setString( "oldName", oldName ) 
.executeUpdate(); 
tx.commit(); 
session.close(); 
 
我按照上面的写法,运行后报错如下:

org.hibernate.QueryException: query must begin with SELECT or FROM: delete [delete Resource r where r.nodeId=:nodeId and r.devId is not null ]
 at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:83)
 at org.hibernate.hql.classic.PreprocessingParser.token(PreprocessingParser.java:108)
 at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:28)
 at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:176)
 at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:152)
 at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
 at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
 at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:865)
 at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:89)


昨天的问题解决了
又把hibernate3的手册仔细的看了一遍,确认自己的写法是没错的,然后又上hibernate的论坛,几乎没有人有我的这个问题,这才想到是不是同事在hibernate.hbm.xml中设置了什么参数,使得我不能使用3.0的特性呢?
      看了,果真如此,郁闷哦~
     里面有一句:
     <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
     这样就使用了2.0的接口.郁闷啊.
     把这句去掉就OK了.
 
     附:
      因为使用了中文拼装的hql后,中文会变成问号,大致如下:
 
      String hql="from user u where u.name like %"+userName+"%";
 
      3.0版本是:
      Query query = session.createQuery("from user u where u.name like :username").setString("username",userName);




               
               
现在要通过一个主键id去数据库中删除对应的一条记录,我该怎样做?如果是将参数delId传递进来的话,那该怎么处理? 我这样做的,但是报了错 public void delfun(String delId) throws DatastoreException { Session session = HibernateUtil.getSession(); Transaction tx = null; try{ tx = session.beginTransaction(); Info info = (Info)session.load(Info.class,id); //出错行 session.delete(delId); tx.commit(); } catch(Exception ex) { HibernateUtil.rollbackTransaction(tx); throw DatastoreException.datastoreError(ex); } finally { HibernateUtil.closeSession(session); } } 错误信息为: com.framework.exceptions.DatastoreException.datastoreError(DatastoreException.java:28) com.service.NpisServiceImpl.delGyinfo(NpisServiceImpl.java:131)