-->
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.  [ 3 posts ] 
Author Message
 Post subject: composite-id and overriding Equals() and GetHashCode()
PostPosted: Mon Apr 03, 2006 3:10 am 
Beginner
Beginner

Joined: Mon Apr 03, 2006 2:41 am
Posts: 25
Location: Mauritius
Hi all,

I'm reading the documentation of NHibernate in order to know if it fits to my needs.

I got objects from a webservice and I want to persist them to a database.

The class are generated from Visual Studio and I would prefer not to modify them.

At the start of my reading, NHibernate seemed to be perfect for my needs because it doesn't require to change the Business object, in my case the generated class from the webservice.

Then I have found this in the documentation about "composite-id" ( almost all primary keys in my database are componed with two columns).

Quote:
Your persistent class must override Equals() and GetHashCode() to implement composite identifier equality. It must also be Serializable.

Unfortunately, this approach to composite identifiers means that a persistent object is its own identifier. There is no convenient "handle" other than the object itself. You must instantiate an instance of the persistent class itself and populate its identifier properties before you can Load() the persistent state associated with a composite key. We will describe a much more convenient approach where the composite identifier is implemented as a seperate class in TODO:LINKTOCOMPENENTS.


In my case, this means I have to modified each generated classes and override the Equals() and GetHashCode() methods. This is a bit disturbing.

Does the last version of NHibernate always contain this limitation ?
is the composite identifier which is implemented as a seperate class already integrated in the last version ?
is this change planned before the end of this month ( I have a short dead line) ?

A last question :
Does the composite identifiers permit me to use the ISession's SaveOrUpdate() method ?

Thanks in advance for your feedback

Regards

Christophe


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 4:09 am 
Senior
Senior

Joined: Thu Aug 25, 2005 3:35 am
Posts: 160
Ok, here goes:

The composite ID _has_ to be it's own class. So I just bet you can't use straight generated classes.
And yes, you have to override equals and hashcode. This is not a limitation of NH, but a consequence of using a class to represent an identifier.

This has worked like this for ages, so I don't quite know what you mean with the 'is this change planned before the end of this month' question. As far as I know, this will not be changed.

The composite identifier does not change the fact that you can use the SaveOrUpdate method, but because NH now does not know how to decide if it should update or insert, you will probably have to tell him yourself. Use an interceptor for that.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 4:20 am 
Beginner
Beginner

Joined: Mon Apr 03, 2006 2:41 am
Posts: 25
Location: Mauritius
Ok thanks for the information.

I have just hoped there is way for me to do this using generated class :(

I will look at "interceptor" as you said.

regards,
Christophe


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