hibernate中session.delete(sql),3.1与2.0的区别,导致的错误
2010-01-21 00:24
471 查看
今天使用session.delete(sql);报下面的错误:
org.hibernate.MappingException: Unknown entity: java.lang.Stringat 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)
相关文章推荐
- hibernate.properties not found(导致Configured SessionFactory: null 错误)
- hibernate中一种导致a different object with the same identifier value was already associated with the session错误方式及解决方法
- Hibernate运行错误之java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getPro
- Hibernate Session中的save(),update(),delete(),saveOrUpdate() 细粒度分析
- HttpSession和HibernateSession的区别
- hibernate 的openSession和getCurrentSession的区别
- SQL truncate table和delete的区别
- Hibernate Session的delete()方法
- HttpSession与Hibernate中Session的区别
- ASP.NET 2.0 创建母版页导致js出现“ 'document.getElementById(...)' 为空或不是对象”错误《转》
- Hibernate 中的 openSession和getCurrentSession 方法的区别
- Hibernate: org.hibernate.exception.SQLGrammarException: could not insert: 错误
- Hibernate session 的evict()和clear()的区别
- Hibernate createSQLQuery与createQuery的区别
- sql中update,alter,modify,delete,drop的区别和使用(整理)(转)
- Hibernate的Session get与load对象区别备忘
- sqlserver update delete 别名的使用 与 oracle 的区别
- 在SQL中Connection, Session, 和Request三个概念的区别
- 自定义ASP.net 2.0 Membership的步骤,和entry 'AspNetSqlMembershipProvider' has already been added错误的解决
- getHibernateTemplate和getSession 区别, this.getHibernateTemplate().getSessionFactory().getCurrentSession()和OpenSession区别