Announcement Announcement Module
Collapse
No announcement yet.
Hibernate and multiple references to the same row in database Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate and multiple references to the same row in database

    Hi everybody,

    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:

    1 - 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.

    2 - 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)

    3 - I then repeat step 2 and store the instance into an audioFile2 object.

    In summary: my main class goes like this:
    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();
    }
    ISSUE: 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)

    I am exposing my problem using a simple Main class, but I am in reality developing a Swing application made of multiples views and controllers, and audioFiles are then retrieved from different objects that are not linked to each other. Thus the same row can be retrieved from two different views (Using two different queries) and it is primordial for me to obtain objects that reference to the same instance.

    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.

    Do any of you know what I am missing here? Maybe it is a HIBERNATE configuration issue?
    Any ideas?

    Thank you in advance. (I can provide a copy of my application-context, service, and dao code if needed)
    Last edited by tom59685; May 3rd, 2012, 09:10 PM.
Working...
X