-->
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.  [ 9 posts ] 
Author Message
 Post subject: Hibernate 3 load than update
PostPosted: Wed Nov 02, 2005 11:50 am 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
We are migrating from Hibernate 2 to Hibernate 3. Everything seems to work fine except when we load an object and than update it. We are using CMT transactions, so we are not managing the sessions. The following is an example:

Code:
    @RolesAllowed({"Sales"})
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    private void updateBooking(long id){
         BookingDAO dao = new BookingDAO();
         Booking booking = null;
         try{
              booking = dao.load(id);
         }
         catch (HibernateException e){e.printstacktrace(); return;}

         if (booking != null){
               booking.setModifyDate(new Date());
               try{
                   dao.update(booking);
               }
               catch (HibernateException e){e.printstacktrace(); return;}
         }
    }


I read that we should use merge(). I also read that rather using load I can use get() and than still do the update(). Both work for me, but my question is which one is right way to do it or does it not matter?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 11:56 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Ugh, bad code. Read the "Working with objects" chapter in the reference documentation and pay special attention to "object states".


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 12:04 pm 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
We know that it was a bad idea, however from we can not open a second session. So I assume we must do a merge(). Is that correct? Another question is that in the example from the reference, how do they get a second session. We can not get the second session because we are not managing sessions. We can not close the session, if we do than we get an error. We are using CMT transaction
Code:
// in the first session
Cat cat = (Cat) firstSession.load(Cat.class, catId);
Cat potentialMate = new Cat();
firstSession.save(potentialMate);

// in a higher layer of the application
cat.setMate(potentialMate);

// later, in a new session
secondSession.update(cat);  // update cat
secondSession.update(mate); // update mate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 12:08 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Nothing to do with sessions. You are simply using the methods all wrong and don't really understand what they do. You don't need load(), update(), or merge(). In fact, all you need is a single get(). It's all explained in the chapter I pointed out.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 12:17 pm 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
If I do a single get(), than how do we specify that the object now needs to be updated?

Suppose this would be the code now:
Code:
@RolesAllowed({"Sales"})
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    private void updateBooking(long id){
         BookingDAO dao = new BookingDAO();
         Booking booking = null;
         try{
              booking = dao.get(id);
         }
         catch (HibernateException e){e.printstacktrace(); return;}

         if (booking != null){
               booking.setModifyDate(new Date());
               //What goes here to update the object .
         }
    }


I am sorry I am having a difficult time with this. We want to load some object change something and than update it. Simply using the get() does not commit the change. What is it that we do to tell it to commit.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 1:45 pm 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
Ok Christian, I see what you mean. Sorry but we were actually doing something else. Our booking object has lots of attributes.

The following works:
Code:
Booking booking = dao.get(id);
booking.setSomething("sdf");


The following does not work:
Code:
   @RolesAllowed({"Sales"})
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    private void updateBooking(Booking newbooking){
         BookingDAO dao = new BookingDAO();
         Booking oldbooking = null;
         try{
              oldbooking = dao.get(id);
         }
         catch (HibernateException e){e.printstacktrace(); return;}

         if (oldbooking != null){
               . . .
               //Do some comparision work, and create some audit

               oldbooking = newbooking; //This does not work. 
         }
    }


How do I update all the attributes without having to explicitly set each attribute? When setting oldbooking = newbooking does not update the changes. Is this when we have to use merge() or there is another way to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 6:56 pm 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
Could someone please shine some light into this subject matter


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 02, 2005 6:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
oldbooking = newbooking; //This does not work.

Wow!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 11:28 am 
Regular
Regular

Joined: Wed Sep 29, 2004 11:34 am
Posts: 62
Location: Houston, TX
So how do we update the object. Do we still use the merge()?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.