08-06
02
Hibernate3:Unknown entity: java.lang.String
作者:Java伴侣 日期:2008-06-02
今天把孙MM的<<精通Hibernate>>里第五章的代码跑了一遍,有一个方法怎么也过不去:
总报错:
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,批量删除的方法也已经改了,现在是:
终于解决了!!!!
复制内容到剪贴板 程序代码
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();
}
}
// 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();
int deletedEntities = session.createQuery( hqlDelete )
.executeUpdate();
终于解决了!!!!
评论: 0 | 引用: 0 | 查看次数: 1731
发表评论