休眠删除查询 [英] Hibernate Delete query

查看:92
本文介绍了休眠删除查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

  session.delete(object)

然后我可以这样:

1)存在于数据库中,然后执行两个SQL查询:选择然后删除

<2>如果数据库中没有行,那么只有select查询是得到执行



但是,更新并非如此。无论DB行的存在,只有更新查询得到执行。



请让我知道为什么这种行为用于删除操作。是不是一个性能问题,因为两个查询被击中而不是一个?



编辑:



我使用hibernate 3.2.5



示例代码

  SessionFactory sessionFactory = new Configuration()。configure(student.cfg.xml)。buildSessionFactory(); 
Session session = sessionFactory.openSession();
学生=新生();
student.setFirstName(AAA);
student.setLastName(BBB);
student.setCity(CCC);
student.setState(DDD);
student.setCountry(EEE);
student.setId(FFF);
session.delete(student);
session.flush();
session.close();

cfg.xml

 < property name =hibernate.connection.username>系统< / property> 
< property name =hibernate.connection.password> XXX< / property>
< property name =hibernate.connection.driver_class> oracle.jdbc.OracleDriver< / property>
< property name =hibernate.connection.url> jdbc:oracle:thin:@localhost:1521 / orcl< / property>
< property name =hibernate.jdbc.batch_size> 30< / property>
< property name =hibernate.dialect> org.hibernate.dialect.OracleDialect< / property>
< property name =hibernate.cache.use_query_cache> false< / property>
< property name =hibernate.cache.use_second_level_cache> false< / property>
< property name =hibernate.connection.release_mode> after_transaction< / property>
< property name =hibernate.connection.autocommit> true< / property>
< property name =hibernate.connection.pool_size> 0< / property>
< property name =hibernate.current_session_context_class>线程< / property>
< property name =hibernate.show_sql> true< / property>
< property name =hibernate.hbm2ddl.auto>更新< / property>

hbm.xml

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate Mapping DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping>
< class name =com.infy.model.Studenttable =STUDENT>
< id name =idcolumn =ID>
< generator class =assigned>< / generator>
< / id>
< property name =firstNametype =stringcolumn =FIRSTNAME>< / property>
< property name =lastNametype =stringcolumn =LASTNAME>< / property>
< property name =citytype =stringcolumn =CITY>< / property>
< property name =statetype =stringcolumn =STATE>< / property>
< property name =countrytype =stringcolumn =COUNTRY>< / property>
< / class>


解决方案

原因是,对于删除一个对象,Hibernate要求该对象处于持久状态。因此,Hibernate首先获取对象(SELECT),然后删除它(DELETE)。



为什么Hibernate需要首先获取对象?原因是可能启用了Hibernate拦截器( http:/ /docs.jboss.org/hibernate/orm/3.3/reference/en/html/events.html ),并且该对象必须通过这些拦截器才能完成其生命周期。如果行直接在数据库中删除,拦截器将不会运行。另一方面,可以使用批量操作删除单个SQL DELETE语句中的实体。

  Query q = session.createQuery(delete Entity where id = X); 
q.executeUpdate();


When I try to delete an entry from a db, using

session.delete(object) 

then I can the following:

1) If the row is present in DB then two SQL queries are getting executed: A select and then a delete

2) If the row is not present in the DB then only the select query is getting executed

But again this is not the case for update. Irrespective of the presence of DB row, only the update query is getting executed.

Please let me know why this kind of behaviour for delete operation. Isn't it a performance issue since two queries are getting hit rather than one?

Edit:

I am using hibernate 3.2.5

Sample code:

SessionFactory sessionFactory = new Configuration().configure("student.cfg.xml").buildSessionFactory();
    Session session = sessionFactory.openSession();
    Student student = new Student();
    student.setFirstName("AAA");
    student.setLastName("BBB");
    student.setCity("CCC");
    student.setState("DDD");
    student.setCountry("EEE");
    student.setId("FFF");
    session.delete(student);
    session.flush();
            session.close();

cfg.xml

<property name="hibernate.connection.username">system</property>
    <property name="hibernate.connection.password">XXX</property>
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521/orcl</property>      
    <property name="hibernate.jdbc.batch_size">30</property>
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="hibernate.cache.use_query_cache">false</property>
    <property name="hibernate.cache.use_second_level_cache">false</property>
    <property name="hibernate.connection.release_mode">after_transaction</property>
    <property name="hibernate.connection.autocommit">true</property>
    <property name="hibernate.connection.pool_size">0</property>
    <property name="hibernate.current_session_context_class">thread</property>    
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>        

hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.infy.model.Student" table="STUDENT">
    <id name="id" column="ID">
        <generator class="assigned"></generator>
    </id>
    <property name="firstName" type="string" column="FIRSTNAME"></property>
    <property name="lastName" type="string" column="LASTNAME"></property>
    <property name="city" type="string" column="CITY"></property>
    <property name="state" type="string" column="STATE"></property>
    <property name="country" type="string" column="COUNTRY"></property>        
</class>

解决方案

The reason is that for deleting an object, Hibernate requires that the object is in persistent state. Thus, Hibernate first fetches the object (SELECT) and then removes it (DELETE).

Why Hibernate needs to fetch the object first? The reason is that Hibernate interceptors might be enabled (http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/events.html), and the object must be passed through these interceptors to complete its lifecycle. If rows are delete directly in the database, the interceptor won't run.

On the other hand, it's possible to delete entities in one single SQL DELETE statement using bulk operations:

Query q = session.createQuery("delete Entity where id = X");
q.executeUpdate();

这篇关于休眠删除查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆