title: Spring, Hibernate and multiple references to the same row in database
body: <p>I'm currently facing a problem here and I thought maybe some of you guys could help me out.
  I am developing an application using SPRING and HIBERNATE and my issue is the following:</p>
  
  <p><strong>1</strong> - I first create a new AudioFile object (Domain class) in my main object, and insert it in the database using the hibernateTemplate.save(audioFile) method in the DAO.</p>
  
  <p><strong>2</strong> - I then use hibernateTemplate.get(1) method of the DAO to retrieve the row from the database, and store the instance into an audioFile1 object. (Of type AudioFile)</p>
  
  <p><strong>3</strong> - I then repeat <strong>step 2</strong> and store the instance into an audioFile2 object.</p>
  
  <p><strong>In summary:</strong> my main class goes like this:</p>
  
  <pre><code>ApplicationContext context = new ClassPathXmlApplicationContext(
                  "application-context.xml");
  AudioFileServices audioFileServices = (AudioFileServices) context.getBean("audioFileServices");
  
  AudioFile audioFile = new AudioFile();
  audioFile.setAlbum("test");
  audioFile.setArtist("test");
  audioFile.setLength(1);
  audioFile.setPath("test");
  audioFile.setTitle("test");
  
  AudioFile a1 = null;
  AudioFile a2 = null;
  
  try {
      audioFileServices.saveAudioFile(audioFile);
      a1 = audioFileServices.getAudioFile(audioFile.getIdAudioFile());
      a2 = audioFileServices.getAudioFile(audioFile.getIdAudioFile());
  } catch (Exception e) {
      e.printStackTrace();
  }
  </code></pre>
  
  <p><strong>ISSUE:</strong> Both audioFile1 and audioFile2 are representing the same object, (The same row in the database) however, they are two different instances and not two objects referencing the same instance. (As I wish it would be) Therefore, any modification made to the first object does not affect the second one. (Even though they are both mapped to the same database row)</p>
  
  <p>I have properly configured the application context to inject the AudioFileDAO bean into the AudioFileService bean and am using the Service layer architecture. Everything works perfectly except for this annoying issue. I have also set up transactions and am using the sessionFactory bean that is injected into the AudioFileDAO bean by SPRING as well.</p>
  
  <p>Do any of you know what I am missing here? Maybe it is a HIBERNATE configuration issue?
  Any ideas?</p>
  
  <p>Thank you in advance. (I can provide a copy of my application-context, service, and dao code if needed)</p>