當前位置:首頁 » 網路管理 » hibernate如何刪除
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

hibernate如何刪除

發布時間: 2022-05-07 06:09:56

A. 在struts和hibernate中怎樣通過id來刪除一記錄

struts負責從頁面上把記錄id獲取到通過參數的形式傳遞到層, 也就是hibernate層,
hibernate是通過id來進行db操作刪除記錄的,刪除方法如下:
publicstatic void delete(Object obj) {
Session session = null;
Transaction tx = null;
try {
// 獲取到session對象
session =HibernateUtil.getSession();
tx =session.beginTransaction();// 開啟事務並且返回該事務對象
session.delete(obj);// 此時處於瞬時狀態
tx.commit();
} finally {// 保證資源得到釋放
if (session != null) {
session.close();
}
}
}
obj是封裝好的對象,裡面還有了傳過來的id,後台直接根據此id刪除記錄。

B. hibernate多對多關聯如何如何刪除中間表的

在set節點的cascade屬性設置值為delete,就可以級聯刪除。

C. hibernate刪除表的方法

你的session變成同一個了,第一次操作成功,第二次就失敗了,因為都是同一個session,
你把getsession方法給成
return
hibernatesessionfactory.getsession();
這樣每次去獲取都是新的。session你不要做成成員變數,使用起來危險,應該做成局部變數,每次都是重新定義,重新獲取。。。

D. 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語句中更新或刪除大批量的數據。

E. 在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();

F. hibernate中的多對一關系時,怎樣刪除

如上圖中的代碼所示,在一對多的關系中找到主控方,然後把它關聯對象的SET集合清空,刪除主控方對象時,Hibernate自動會刪除級聯的對象,祝你好運。

G. hibernate的刪除問題

當然啊。。。你添加後沒有提交事物,屬於臨時狀態,當然刪除不了。

可以在添加和刪除的方法里寫提交事物的動作。
或者把hibernate 設置成自動提交,在hibernate.cfg.xml中增加屬性connection.autocommit='true'

不過建議使用第一種,如果出錯的話在catch中可以回滾,

H. 怎樣徹底刪除已經添加好的spring和hibernate框架

首先將spring和hibernate的jar包全部刪掉,之後將向關聯的xml配置文件刪除,
如果你的DAO中用到Hibernate的Session,當你把jar包刪掉的時候,可能會報錯,
你要適當的修改你的代碼.
如果你的spring在web.xml中配置過呢,也要將配置語句刪掉.
基本好像就這么多

I. hibernate如何刪除數據

"拼串"形成的HQL語句的寫法,其能夠形成一條語句,從而效率得到最大的提升。

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

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

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

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