2006/5/16

hibernate中session.delete(sql),3.1與2.0的區別,導致的錯誤

今天使用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)
回復人:pdvv(我愛花貓) 2006-3-1 17:32:22得分:0
?
你給出的異常應該屬于你自己框架的信息吧,沒有多少參考價值。 使用Session.load()要確保數據一定存在,還是用get()吧; 或者直接session.delete("from INFO where ID = 1");
Top
回復人:yuanjian0211(元劍) 2006-3-1 17:46:04得分:0
?
錯誤提示信息為: Hibernate: select gyinfo0_.id as id0_, info0_.userid as userid0_, info0_.tit le as title0_,info0_.descript as descript0_ from Info info0_ where info0_.id=? 請指點
Top
回復人:yuanjian0211(元劍) 2006-3-1 17:56:29得分:0
?
id在數據庫表中為bigint型 在對象中為long型
Top
回復人:iori_powermax() 2006-03-02 01:06:00得分:0
?
我也在學習hibernate,不知道說的對不對. 你刪除的應該是整個對象,而不是一個id,所以應該為: Info info = (Info)session.load(Info.class,id); session.delete(info); 或者還有一種刪除記錄的方法: info=(Info)sn.creatQuery("from Info as a where a.ID= '"+delId+"'").uniqueResult(); sn.delete(info); 另外想問一下大家: hibernate的creatSQLQuery()是不是只支持查詢?好象增刪改都不可以
Top
回復人:ymfhcn(這痞子真帥) 2006-03-02 08:06:00得分:0
?
creatSQLQuery()是執行sql語句的,不過稍微有些改動,里面什么SQL語句都可以寫



?| = | 一般分類 | 仙俠修真 | VBScript | .NET | 面試題 | 存儲過程 | Hibernate | JavaScript | J2EE | Struts
自我介紹
切換風格
訂閱我的Blog
博客日歷
文章歸檔...
最新發表...
博客統計...
網站鏈接...
資源
===========================================================
Hibernate 的 session.delete(obj
===========================================================

session.delete(obj)將obj的狀態變為transient。兩種情況
1)obj是session的cache里邊的cache沒有的,比如:
session.delete(new Employee(4));
2)obj存在于session的cache中,比如:
Employee employee = (Employee)session.load(Employee.class, new Integer(4));
session.delete(employee);

這兩種情況都是允許的,hibernate都會發送一條delete語句給數據庫。

delete執行之后,如果調用了session.load(), 又可以分為兩種情況:1)在session.flush()之前,如:
tx.beginTransaction();
    session.delete(new Employee(4));
session.load(Employee.class, new Integer(4));//發生在session.flush()之前
tx.commit();
那么hibernate會拋出ObjectDeletedException:The object with that id was deleted:

2)在session.flush()之后,如:
tx.beginTransaction();
    session.delete(new Employee(4));
session.load(Employee.class, new Integer(4));
tx.commit();

tx.beginTransaction();
session.load(Employee.class, new Integer(4));//同一個session中,上面的tx.commit()將session flush了一次。
tx.commit();
那么這個時候hibernate僅僅會拋出ObjectNotFoundException:No row with the give...
表示找不到該object。如果第二個tx里邊采用session.get()也就不會拋出exception了。

delete執行之后,如果調用了session.save(obj):
tx.beginTransaction();
Employee employee = (Employee)session.load(Employee.class, new Integer(4));
    session.delete(employee);
System.out.println(employee);
session.save(employee);
System.out.println(employee);
tx.commit();
這種情況是完全合理的,合法的。
delete將employee從persistent的狀態變為transient的狀態。
save將employee從transient狀態變為persistent的狀態。
save一個被delete的obj的時候,在save處hibernate強制執行session.flush(),發送delete語句,然后按照常規的save流程來進行。為什么要這么做,還沒有完全想明白。

delete執行之后,如果對obj對象屬性的修改,tx.commit()時不會進行dirtyChecking。
這個道理比較顯然。

- 作者: HairRoot 2005年05月




hibernate連接sqlserver2000問題的解決

這幾天在進行數據庫的移植,將oracle數據庫的東西移植到mssqlserver 2000上
?
其中運行一個查詢的時候,報如下的錯誤:
?
?[Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
?
解決辦法:
?? <property name="connection.url">
?? ? jdbc:microsoft:sqlserver://192.168.1.110:2433;SelectMethod=cursor
? </property>
?
(紅色為增加的)
?
原因:
?? 1.??? 處于手動事務狀態,并且使用direct模式,因為hibernate默認為direct模式,
???????? 即 SelectMethod=direct
?? 2.? ? 在一個SQL SERVER的JDBC連接上執行多個STATEMENTS的操作


超強的貼,每句都是經典
水至清則無魚,人至賤則無敵! 走自己的路,讓別人打車去吧。穿別人的鞋,走自己的路,讓他們找去吧。打臺灣我捐一個月的生活費,打美國我捐一年的生活費, 打日本我捐他媽的一條命! 我不是隨便的人,我隨便起來不是人。女人無所謂正派,正派是因為受到的引誘不夠;男人無所謂忠誠,忠誠是因為背叛的籌碼太低…… 騎白馬的不一定是王子,可能是唐僧;帶翅膀的也不一定是天使,有時候是鳥人。 俺的最低奮斗目標:農婦,山泉,有點田。再過幾十年,我們來相會,送到火葬場,全部燒成灰,你一堆,我一堆,誰也不認識誰,全部送到農村做化肥




http://iceling2008.itpub.net/category/13270/23134好東西???????? 阿法薩
http://comsmall.spaces.live.com/PersonalSpace.aspx?_c02_owner=1


遇到 "Automation服務器不能創建對象"

運行 Regsvr32 scrrun.dll。