Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate Delete query
    primarykey
    data
    text
    <p>When I try to delete an entry from a db, using </p> <pre><code>session.delete(object) </code></pre> <p>then I can the following:</p> <p>1) If the row is present in DB then two SQL queries are getting executed: A select and then a delete</p> <p>2) If the row is not present in the DB then only the select query is getting executed</p> <p>But again this is not the case for update. Irrespective of the presence of DB row, only the update query is getting executed.</p> <p>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?</p> <p><strong>Edit:</strong> </p> <p>I am using hibernate 3.2.5</p> <p><strong>Sample code:</strong></p> <pre><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(); </code></pre> <p><strong>cfg.xml</strong></p> <pre><code>&lt;property name="hibernate.connection.username"&gt;system&lt;/property&gt; &lt;property name="hibernate.connection.password"&gt;XXX&lt;/property&gt; &lt;property name="hibernate.connection.driver_class"&gt;oracle.jdbc.OracleDriver&lt;/property&gt; &lt;property name="hibernate.connection.url"&gt;jdbc:oracle:thin:@localhost:1521/orcl&lt;/property&gt; &lt;property name="hibernate.jdbc.batch_size"&gt;30&lt;/property&gt; &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.OracleDialect&lt;/property&gt; &lt;property name="hibernate.cache.use_query_cache"&gt;false&lt;/property&gt; &lt;property name="hibernate.cache.use_second_level_cache"&gt;false&lt;/property&gt; &lt;property name="hibernate.connection.release_mode"&gt;after_transaction&lt;/property&gt; &lt;property name="hibernate.connection.autocommit"&gt;true&lt;/property&gt; &lt;property name="hibernate.connection.pool_size"&gt;0&lt;/property&gt; &lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt; &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt; &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt; </code></pre> <p><strong>hbm.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt; &lt;hibernate-mapping&gt; &lt;class name="com.infy.model.Student" table="STUDENT"&gt; &lt;id name="id" column="ID"&gt; &lt;generator class="assigned"&gt;&lt;/generator&gt; &lt;/id&gt; &lt;property name="firstName" type="string" column="FIRSTNAME"&gt;&lt;/property&gt; &lt;property name="lastName" type="string" column="LASTNAME"&gt;&lt;/property&gt; &lt;property name="city" type="string" column="CITY"&gt;&lt;/property&gt; &lt;property name="state" type="string" column="STATE"&gt;&lt;/property&gt; &lt;property name="country" type="string" column="COUNTRY"&gt;&lt;/property&gt; &lt;/class&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload