title: org.hibernate.ObjectNotFoundException: No row with the given identifier exists, but it DOES
body: <p>I've got a hibernate problem that I can't fix.</p>
  
  <p>The setup: Java EE, web app, Hibernate 3.2, Tomcat 6, Struts 2.</p>
  
  <p>Basically, I persist an object with my server logic (a struts action), then try and pull that data out for the next page and display it.</p>
  
  <p>I check the database after I save the object, and sure enough, I can see the row there with all the data.</p>
  
  <p>But when I try and retrieve it I get this:</p>
  
  <pre><code>org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [msc.model.Picture#73]
  </code></pre>
  
  <p>To make things even muddier, when I restart Tomcat and try and access the same object, I don't get the error - Hibernate finds the row just fine.</p>
  
  <p>Hibernate will also be able to see the row if I do some other operations - maybe add a row here and there to the database, not even on the same table.</p>
  
  <p>From all this I suspect a Hibernate bug, but I'm a Hibernate newbie so I am probably wrong. Please help! I've googled and googled to no avail.</p>
  
  <p>Here is my Hibernate config:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
          &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/msc&lt;/property&gt;
          &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
          &lt;property name="hibernate.connection.password"&gt;-------&lt;/property&gt;
          &lt;!-- JDBC connection pool (use the built-in) --&gt;
          &lt;property name="connection.pool_size"&gt;80&lt;/property&gt;
          &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
          &lt;mapping resource="msc/model/Picture.hbm.xml"/&gt;
          &lt;mapping resource="msc/model/Comment.hbm.xml"/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>Here are my two mapping files:</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="msc.model.Picture" table="PICTURE"&gt;
      &lt;id column="PICTURE_ID" name="id"&gt;
        &lt;generator class="native"/&gt;
      &lt;/id&gt;
      &lt;property name="story"/&gt;
      &lt;property name="email"/&gt;
      &lt;property name="category"/&gt;
      &lt;property name="state"/&gt;
      &lt;property name="ratings"/&gt;
      &lt;property name="views"/&gt;
      &lt;property name="timestamp"/&gt;
      &lt;property name="title"/&gt;
      &lt;property lazy="true" name="image" type="blob"&gt;
        &lt;column name="IMAGE"/&gt;
      &lt;/property&gt;
      &lt;property lazy="true" name="refinedImage" type="blob"&gt;
        &lt;column name="REFINEDIMAGE"/&gt;
      &lt;/property&gt;
      &lt;property lazy="true" name="thumbnail" type="blob"&gt;
        &lt;column name="THUMBNAIL"/&gt;
      &lt;/property&gt;
      &lt;bag cascade="save-update" lazy="true" name="comments" table="COMMENT"&gt;
        &lt;key column="PICTURE"/&gt;
        &lt;one-to-many class="msc.model.Comment"/&gt;
      &lt;/bag&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>and</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="msc.model.User" table="USER"&gt;
      &lt;id column="USER_ID" name="id"&gt;
        &lt;generator class="native"/&gt;
      &lt;/id&gt;
      &lt;property name="username"/&gt;
      &lt;property name="email"/&gt;
      &lt;bag cascade="save-update" lazy="true" name="pictures" table="PICTURE"&gt;
        &lt;key column="USER"/&gt;
        &lt;one-to-many class="msc.model.Picture"/&gt;
      &lt;/bag&gt;
      &lt;bag cascade="save-update" lazy="true" name="comments" table="COMMENT"&gt;
        &lt;key column="USER"/&gt;
        &lt;one-to-many class="msc.model.Comment"/&gt;
      &lt;/bag&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Please let me know if you need more info, I'm happy to oblige.</p>
  
  <p>(note: this is not a duplicate of this question, the scenario is not the same <a href="https://stackoverflow.com/questions/2847231/no-row-with-the-given-identifier-exists-although-it-does-exist">&quot;No row with the given identifier exists&quot; although it DOES exist</a>)</p>
  
  <p><strong>EDIT</strong>: as requested, posting Java code:</p>
  
  <p>Code to save object</p>
  
  <pre><code>Session hib_ses = HibernateUtil.getSessionFactory().getCurrentSession();
  
  hib_ses.beginTransaction();
  
  hib_ses.save(user);
  
  hib_ses.getTransaction().commit(); 
  </code></pre>
  
  <p>Code to display data (an image in this case)</p>
  
  <pre><code>public class ImageAction extends ActionSupport implements ServletResponseAware, SessionAware {
  
      private HttpServletResponse response;
      Map session;
      private Long id;
      private int thumbnail;
      private InputStream inputStream;
  
      @Override
      public String execute() throws Exception {
          response.setContentType("image/jpeg");
          Session hib_session = HibernateUtil.getSessionFactory().getCurrentSession();
          hib_session.beginTransaction();
  
          //what is the ID now?
          Picture pic = (Picture) hib_session.load(Picture.class, getId());
  
          if (thumbnail == 1) {
              inputStream = (ByteArrayInputStream) pic.getThumbnail().getBinaryStream();
          } else {
              inputStream = (ByteArrayInputStream) pic.getRefinedImage().getBinaryStream();
          }
  
          hib_session.close();
          return SUCCESS;
      }
  </code></pre>