当前位置:首页 » 网络管理 » 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对象进行操作进而影响数据库。