I’m developing an app through which users can send email messages with attachments. Both Email and Attachment domain objects have hilo defined as id generator as follows:
<id name="Id">
<generator class="hilo" />
</id>
Nhibernate generates schema with table named hibernate_unique_key with columns next_hi.
When user adds attachment to the email, internally app adds attachment object to list of attachments and binds it to grid view so users can see what they added.
Optionally users can select previously added attachment and remove it from the list by clicking remove button.
The problem is, since non of the objects are saved to database, id’s of attachments have not been assigned so I cant uniquely identify attachment obj to remove from list.
Is there a way to assign id value to object before saving it? I guess I don’t quite understand usage of hilo algorithm and it’s main purpose.
HiLo is used so that the identifier can be assigned without a roundtrip to the database. What you need to do is something like this (you will need to cater for removing attachments and exception handling etc):
Here are a couple of links that may also help you understand HiLo and NHibernate a bit better:
http://ayende.com/blog/3915/nhibernate-avoid-identity-generator-when-possible
http://nhforge.org/blogs/nhibernate/archive/2009/03/20/nhibernate-poid-generators-revealed.aspx