-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to get cascade updates to work w/ composite ids.
PostPosted: Sat Feb 21, 2004 5:40 pm 
Newbie

Joined: Sat Feb 21, 2004 4:27 pm
Posts: 2
Location: NJ, USA
I'm putting together an application that uses composite keys for each of the objects. The objects are versioned so basically all the keys look like:

( long seq, int version )

To do this I followed the steps recommended elsewhere and use an id declaration of:

Code:
  <composite-id name="primaryKey" unsaved-value="any">
     <key-property name="id" column="id"/>
     <key-property name="version" column="version"/>
  </composite-id>


and am setting using a SequenceGenerator to generate my key prior to calling session.save ().

The problem, of course, is that Hibernate has no clue when an object is saved or needs saving/updating.

How can I get it to behave properly in sess.save of a linked object?

I've come up with the following solution that seems to work but it seems quite inelegant. Is this the right or only way?

1) I created an implementation of Interceptor and add it to each session that i use

2) I updated the primary key object that I'm using in my classes to include a 'saved' bit that I can tweak:

Code:
    public class FooKVO {
      public long id; // persisted
      public int version; // persisted
      public boolean saved; // transient
    }


3) in Interceptor.onLoad and Interceptor.onSave i set that bit for applicable objects using:

Code:
     if (o intsanceof Foo) {
       final Foo foo = (Foo) o;
       foo.getPrimaryKey ().setSaved (true);
    }
    return;


4) I implemented Interceptor.isUnsaved as:

Code:
     if (o intsanceof Foo) {
       final Foo foo = (Foo) o;
       if (foo.getPrimaryKey ().getSaved ()) {
         return Boolean.FALSE;
      }
    }
    return;



Any thoughts or guidance on this?

_________________
--lee


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 21, 2004 9:00 pm 
Regular
Regular

Joined: Tue Sep 09, 2003 9:37 pm
Posts: 56
Location: Ogden, Utah, USA
I don't understand your purpose in making the version stamp part of your key.

Hibernate supports a version tag, which will check versions each time you update (by doing an update where version=loaded version and throw a StaleObjectException when the version has changed since you loaded the object).

This wouldn't be part of the key though.

There are two ways (that I know of!) to make an object with a composite key cascade appropriately - first, write an interceptor and use the isUnsaved function to tell Hibernate which objects are new and which aren't. The other (and easier in my opinion) way is to use the version tag, which will cause hibernate to know the object is unsaved when the version has not yet been assigned.

Good luck,
Jenica


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