title: Hibernate Session.delete() an object if exists
body: <p>In JavaDoc of Session class the description of <a href="http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/Session.html#delete%28java.lang.Object%29" rel="noreferrer">delete</a> method is:</p>
  
  <blockquote>
    <p>Remove a <strong>persistent instance</strong> from the datastore. The argument may be an instance associated with the receiving Session or a transient instance with an identifier associated with existing persistent state.</p>
  </blockquote>
  
  <p>My questions are: </p>
  
  <ol>
  <li>I want to delete a detach object, can I use this method, AFAIK session first makes an object persistent from detach and then perform its operation. Am I right? </li>
  <li>If I am not sure about the existence of the object in database, should I use Session.get() to check the whether or not it is null and then perform delete operation or I can use directly delete operation? </li>
  </ol>
  
  <p>Here is a code snippet:</p>
  
  <pre><code>public void removeUnallocatedUserIfExits(final long itemId) {
      getHibernateTemplate().execute(new HibernateCallback() {
  
          public Object doInHibernate(Session session) throws HibernateException, SQLException {
              session.flush();
              session.setCacheMode(CacheMode.IGNORE);
              UnallocatedUser unallocatedUser;
              if ((unallocatedUser = (UnallocatedUser) session.get(UnallocatedUser.class, itemId)) != null) {
                  session.delete(unallocatedUser);
              }
              session.flush();
              return null;
          }
      });
  }
  </code></pre>
  
  <p>Is the okay? </p>