當前位置:首頁 » 操作系統 » hibernate刪除資料庫

hibernate刪除資料庫

發布時間: 2022-08-28 00:47:23

① 在Hibernate3中怎樣實現多條件批量刪除

Hibernate3中可以直接通過面向對象的形式進行條件刪除,或者是直接sql的形式進行批量刪除。

Sql代碼:
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String strSQL=" delete from Classes as a where a.classno like :name";
Query query = session.createQuery(strSQL);
query.setString("name", "%"+OId+"%");
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String strSQL="from Classes as a where a.classno like :name";
Query query = session.createQuery(strSQL);

HQL代碼:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("delete from Classes as a where a.classno
like " '%"+OId+"%'").list();
[sql] view plainprint?
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result=session.createQuery("from Classes as a where a.classno like " '%"+OId+"%'").list();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String result=session.createQuery("delete from Classes as a where a.classno like " '%"+OId+"%'").list();

② 請問如何用HQL語句刪除MySql資料庫

hibernate是做orm映射用的,屏蔽的資料庫的差異,組裝好bean以後,直接insert就行了,hibernate會根據配置文件中的資料庫方言自己組裝sql語句操作資料庫呢

③ Hibernate中如何做批量刪除

批量刪除雖然在hibernate里也可以,但他卻是一個一個刪除,在數量大的情況下很影響效率,昨天在網站上看到了個更好的方法,原來hibernate也提供了JDBC介面,實在是太方便了。

把他cope過來了:

批量更新是指在一個事務中更新大批量數據,批量刪除是指在一個事務中刪除大批量數據。以下程序直接通過Hibernate API批量更新CUSTOMERS表中年齡大於零的所有記錄的AGE欄位:

tx = session.beginTransaction();Iterator customers=session.find("from Customer c where c.age>0").iterator();while(customers.hasNext()){Customer customer=(Customer)customers.next();customer.setAge(customer.getAge()+1);}tx.commit();session.close();

如果CUSTOMERS表中有1萬條年齡大於零的記錄,那麼Session的find()方法會一下子載入1萬個Customer對象到內存。當執行tx.commit()方法時,會清理緩存,Hibernate執行1萬條更新CUSTOMERS表的update語句:

update CUSTOMERS set AGE=? …. where ID=i;update CUSTOMERS set AGE=? …. where ID=j;……update CUSTOMERS set AGE=? …. where ID=k;

以上批量更新方式有兩個缺點:

(1)佔用大量內存,必須把1萬個Customer對象先載入到內存,然後一一更新它們。
(2)執行的update語句的數目太多,每個update語句只能更新一個Customer對象,必須通過1萬條update語句才能更新一萬個Customer對象,頻繁的訪問資料庫,會大大降低應用的性能。

為了迅速釋放1萬個Customer對象佔用的內存,可以在更新每個Customer對象後,就調用Session的evict()方法立即釋放它的內存:

tx = session.beginTransaction();Iterator customers=session.find("from Customer c where c.age>0").iterator();while(customers.hasNext()){Customer customer=(Customer)customers.next();customer.setAge(customer.getAge()+1);session.flush();session.evict(customer);}tx.commit();session.close();

在以上程序中,修改了一個Customer對象的age屬性後,就立即調用Session的flush()方法和evict()方法,flush()方法使Hibernate立刻根據這個Customer對象的狀態變化同步更新資料庫,從而立即執行相關的update語句;evict()方法用於把這個Customer對象從緩存中清除出去,從而及時釋放它佔用的內存。

但evict()方法只能稍微提高批量操作的性能,因為不管有沒有使用evict()方法,Hibernate都必須執行1萬條update語句,才能更新1萬個Customer對象,這是影響批量操作性能的重要因素。假如Hibernate能直接執行如下SQL語句:

update CUSTOMERS set AGE=AGE+1 where AGE>0;

那麼,以上一條update語句就能更新CUSTOMERS表中的1萬條記錄。但是Hibernate並沒有直接提供執行這種update語句的介面。應用程序必須繞過Hibernate API,直接通過JDBC API來執行該SQL語句:

tx = session.beginTransaction();Connection con=session.connection();PreparedStatement stmt=con.prepareStatement("update CUSTOMERS set AGE=AGE+1 "+"where AGE>0 ");stmt.executeUpdate();tx.commit();

以上程序演示了繞過Hibernate API,直接通過JDBC API訪問資料庫的過程。應用程序通過Session的connection()方法獲得該Session使用的資料庫連接,然後通過它創建PreparedStatement對象並執行SQL語句。值得注意的是,應用程序仍然通過Hibernate的Transaction介面來聲明事務邊界。

如果底層資料庫(如Oracle)支持存儲過程,也可以通過存儲過程來執行批量更新。存儲過程直接在資料庫中運行,速度更加快。在Oracle資料庫中可以定義一個名為batchUpdateCustomer()的存儲過程,代碼如下:

create or replace procere batchUpdateCustomer(p_age in number) asbeginupdate CUSTOMERS set AGE=AGE+1 where AGE>p_age;end;

以上存儲過程有一個參數p_age,代表客戶的年齡,應用程序可按照以下方式調用存儲過程:

tx = session.beginTransaction();Connection con=session.connection();String procere = "{call batchUpdateCustomer(?) }";CallableStatement cstmt = con.prepareCall(procere);cstmt.setInt(1,0); //把年齡參數設為0cstmt.executeUpdate();tx.commit();

從上面程序看出,應用程序也必須繞過Hibernate API,直接通過JDBC API來調用存儲過程。

Session的各種重載形式的update()方法都一次只能更新一個對象,而delete()方法的有些重載形式允許以HQL語句作為參數,例如:

session.delete("from Customer c where c.age>0");

如果CUSTOMERS表中有1萬條年齡大於零的記錄,那麼以上代碼能刪除一萬條記錄。但是Session的delete()方法並沒有執行以下delete語句:

delete from CUSTOMERS where AGE>0;

Session的delete()方法先通過以下select語句把1萬個Customer對象載入到內存中:

select * from CUSTOMERS where AGE>0;

接下來執行一萬條delete語句,逐個刪除Customer對象:

delete from CUSTOMERS where ID=i;delete from CUSTOMERS where ID=j;……delete from CUSTOMERS where ID=k;

由此可見,直接通過Hibernate API進行批量更新和批量刪除都不值得推薦。而直接通過JDBC API執行相關的SQL語句或調用相關的存儲過程,是批量更新和批量刪除的最佳方式,這兩種方式都有以下優點:

(1)無需把資料庫中的大批量數據先載入到內存中,然後逐個更新或修改它們,因此不會消耗大量內存。
(2)能在一條SQL語句中更新或刪除大批量的數據。

④ hibernate刪除操作,一般如何實現

一般是使用hql來刪除,因為這樣執行性能要高一點,不需要先查詢。

但是如果你配置了hibernate的級聯關系,要進行級聯刪除,就需要使用delete方法了

hibernate用的是hql而不是sql,sql是對資料庫中的表進行操作,hql是對pojo對象進行操作進而影響資料庫。

⑤ 怎樣防止hibernate載入時刪除資料庫表的數據

hibernate有緩存,強制刷新hibernate的緩存看數據是否正確,如果還不行,只能把查詢也加入事務處理機制,就沒有問題了。

⑥ 使用hibernate的delete刪除數據時、資料庫中的數據被刪除了、可還是出現了錯誤

可能是你刪除數據之後在前台並沒有刷新,而此時又在一次進行刪除。這時資料庫里已經沒有該條數據,就會報上面的所錯誤。

⑦ 如何使用hibernate清空資料庫表

你用的是什麼軟體? 如果是Mysql的話 就進入Mysql裡面,然後use空格加你要刪的表所在的資料庫(進入該資料庫),進入該資料庫以後最好 show tables 查看一下你要刪除的表是不是在這裡面,有的話 就繼續輸入 drop空格加你要刪的表的名字,記住分號哈.

⑧ hibernate.hbm2ddl.auto create 不小心把資料庫刪了怎麼恢復

1、沒有刪除資料庫文件(*.mdb,*.ldb)的話重新附加一遍就可以了
2、要是刪除了資料庫文件的話,用數據恢復軟體,Fanl Date或者DG把資料庫文件恢復出來再附加進去就可以了

熱點內容
linux下php編譯安裝 發布:2025-05-15 08:30:37 瀏覽:591
c語言八進制十六進制 發布:2025-05-15 08:22:17 瀏覽:282
華為安卓如何更新鴻蒙 發布:2025-05-15 08:18:52 瀏覽:373
工商密碼器是什麼 發布:2025-05-15 08:18:50 瀏覽:751
c語言自考 發布:2025-05-15 07:52:42 瀏覽:501
壓縮的玉 發布:2025-05-15 07:51:22 瀏覽:790
android的控制項 發布:2025-05-15 07:50:36 瀏覽:553
南崗法院伺服器ip地址 發布:2025-05-15 07:46:02 瀏覽:288
實況如何退出賬號安卓 發布:2025-05-15 07:45:56 瀏覽:919
深入編譯器 發布:2025-05-15 07:41:35 瀏覽:879