-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Loading inheritence classes
PostPosted: Mon Jun 28, 2004 11:17 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
Hi Guys,

I have a parent object A and a child B extending A. I have managed to get Hibernate to persist this inheritence hierarchy to my DB using a joined-subclass configuration.

I have been able to save an object of type B with a method like this

Code:
   public void saveSchedule(A a) {

      try {
         Session session = HibernateUtil.currentSession();
         Transaction tx = session.beginTransaction();
         
         session.save(a);
         
         tx.commit();
         HibernateUtil.closeSession();
         
      } catch (HibernateException hE) {
         logger.error(hE + " " + hE.getMessage());
      }
   }


This works even when the parameter a is of class B.

I now want to load a class out of the database but I want to keep it general. I came up with this...

Code:
   public void loadSchedule(A a, int id) {
      
      try {
         Session session = HibernateUtil.currentSession();
         Transaction tx = session.beginTransaction();

         a= (A) session.load(schedule.getClass(), new Integer(id));

         tx.commit();
         
      } catch (HibernateException hE) {
         logger.error(hE.getMessage());
      }
   }   


I also tried using A.class for schedule.getClass but it always returns just an A and not a B even if the parameter A a is of class B.

I hope this makes sense and you see what I am trying to achieve. I do not want to have to write methods loadScheduleA and loadScheduleB

Thanks! ADC


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 11:35 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
Hm, OK, what I was doing was OK, the problem is that I have 2 calls to my logic object and I am getting an error;

ScheduleLogic.loadSchedule(Unknown Source) : a different object with the same identifier value was already associated with the session: 2, of class:

I have 2 test calls to 2 separate methods in my logic object

Code:
request.setAttribute("aSchedule", logic.loadSchedule(2));


and

Code:
Schedule schedule2 = new DataAcquisitionSchedule();
logic.loadSchedule(schedule2, 2);


I copy the code for these methods below. As far as I can see, they are 2 totally separate calls to methods so why is this causing the error? Is it that the currentSession is already in use when the second call goes in? If I change the first method call to 1 both statements work.

I would probably never need to load the same object sequentially like this, but I would like to understand what is happening here as it seems to me that this cannot be right - that is, surely a sequence of operations on 1 row is quite common??

Code:
   /**
    * Retrive a Schedule from ID.
    *
    */
   public Schedule loadSchedule(int id) {

      Schedule schedule = null;
      
      try {
         Session session = HibernateUtil.currentSession();
         Transaction tx = session.beginTransaction();

         schedule = (Schedule) session.load(Schedule.class, new Integer(id));

         tx.commit();
         
      } catch (HibernateException hE) {
         logger.error(hE.getMessage());
      }
      
      return schedule;
   }   
   
   /**
    * Retrive a Schedule into a passed Schedule.
    *
    */
   public void loadSchedule(Schedule schedule, int id) {
      
      try {
         Session session = HibernateUtil.currentSession();
         Transaction tx = session.beginTransaction();

         session.load(schedule, new Integer(id));

         tx.commit();
         
      } catch (HibernateException hE) {
         logger.error(hE.getMessage());
      }
   }   

Thanks guys, ADC.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 12:50 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You can not have 2 instances representing the same row, so using
load(Object, Serializable) will try to populate the Object instance. If one is already loaded by the session, an exception will be thrown

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 3:52 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
Hi,

Thanks, but I do not entirely understand. In my test I call 2 separate methods one after the other. Each method attempts to load the object with ID 2 in a different way. However, I get this error.

The reason I do not understand what you are saying is that when method 1 has finished loading the object, the current session is no longer used.

From what I understand of what you have said, in a web application I can get object 2 loaded and put into the request object for a JSP to render somehow, but then that object is lost to me, but if I try to then reload it again, Hibernate will complain that it was already loaded. Do I therefore have to make sure I always keep instances loaded by Hibernate somewhere so that I do not have to reload them?

Or should I stop using HibernateUtil so that I create a new session for every method I have for loading?

Thanks and sorry for the confusion.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 8:24 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I may be wrong,. You probably do something like that

load A with session1
close session1

load B with session2
somehow (cascade etc) you load A' (ie same id as A) in session2
you reattach A to session2, since A.id==A'.id, hibernate conplains

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 8:30 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
Here is what I do exactly. My Servlet does the following:

Code:
request.setAttribute("aSchedule1", logic.loadSchedule(2));
Schedule schedule2 = new DataAcquisitionSchedule();
logic.loadSchedule(schedule2, 2);
request.setAttribute("aSchedule2", schedule2);


The calls are

Code:
   /**
    * Retrive a Schedule from ID.
    *
    */
   public Schedule loadSchedule(int id) {

      Schedule schedule = null;
      
      try {
         Session session = HibernateUtil.currentSession();
         Transaction tx = session.beginTransaction();

         schedule = (Schedule) session.load(Schedule.class, new Integer(id));

         tx.commit();
         
      } catch (HibernateException hE) {
         logger.error(hE.getMessage());
      }
      
      return schedule;
   }   
   
   /**
    * Retrive a Schedule into a passed Schedule.
    *
    */
   public void loadSchedule(Schedule schedule, int id) {
      
      try {
         Session session = HibernateUtil.currentSession();
         Transaction tx = session.beginTransaction();

         session.load(schedule, new Integer(id));

         tx.commit();
         
      } catch (HibernateException hE) {
         logger.error(hE.getMessage());
      }
   }   


That's it. I do not see what is wrong with loading Schedule with ID=2 twice in different sessions.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 8:41 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You load it twice in the same session for sure. And the second load fail

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.