Document number generation

1 view
Skip to first unread message

Mauro Servienti

unread,
Aug 28, 2009, 10:36:21 AM8/28/09
to nhu...@googlegroups.com

Hi all,

 

I’m using NH to map a domain that contains some entities that have an “identification number”, it is not a primary key, it’s just an id number that has a meaning for the user. A really simple example is the invoice number.

 

The important thing is that this id number is always generated and never assigned by the user. Each entity has its own strategy to generate this “id number” that has to be generated only at insert time and then is immutable, which is the best strategy to inject in NH this generation logic? Is an IInterceptor a viable way?

 

The simplest generation strategy, just for the records, is the “invoice number”:

-          Within a transaction…

-          Just before insert a transient invoice look into invoices table sorting invoices by number descending and filtering by incoming document year;

-          Take the top number + 1 if one, otherwise take 1;

-          Assign it to the invoice entity;

-          Commit transaction;

 

I know that I can do all this in my code but I would like to automate the process at the persistence level.

 

TIA, best regards.

.m

William Chang

unread,
Aug 28, 2009, 11:53:20 AM8/28/09
to nhusers
I don't think you need to inject into the NHibernate process or some
kind of intercetor. I think it's good to do it in your code, be sure
to make your code has it's own class with a static method (which query
the database for the next id). Another option, you can try implement
your own custom NHibernate generator, but I don't know if you can use
it without being a primary key.

A quick search on Google:
http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-custom-id-generator-for-nhibernate-1

Sincerely,
William Chang

On Aug 28, 10:36 am, "Mauro Servienti" <mauro.servie...@gmail.com>
wrote:
> Hi all,
>
> I'm using NH to map a domain that contains some entities that have an
> "identification number", it is not a primary key, it's just an id number
> that has a meaning for the user. A really simple example is the invoice
> number.
>
> The important thing is that this id number is always generated and never
> assigned by the user. Each entity has its own strategy to generate this "id
> number" that has to be generated only at insert time and then is immutable,
> which is the best strategy to inject in NH this generation logic? Is an
> IInterceptor a viable way?
>
> The simplest generation strategy, just for the records, is the "invoice
> number":
>
> -          Within a transaction.

Mauro Servienti

unread,
Aug 28, 2009, 12:01:39 PM8/28/09
to nhusers
Hi,

first of all thanks for your reply

On Aug 28, 5:53 pm, William Chang <diehardb...@gmail.com> wrote:
> I don't think you need to inject into the NHibernate process or some
> kind of intercetor. I think it's good to do it in your code, be sure
> to make your code has it's own class with a static method (which query
> the database for the next id).

That is really similar to what I am doing now.

> Another option, you can try implement
> your own custom NHibernate generator, but I don't know if you can use
> it without being a primary key.
>
> A quick search on Google:http://www.lucisferre.org/coding/index.php/2009/07/13/implementing-a-...
>

Well that code, imho, isn't really a good sample on how to build a
generator. I've done a couple of generetors and I think there are
better options.
But this is not the point, as you point out generators works only for
PK.

> Sincerely,
> William Chang
>

.m

Raul Carlomagno

unread,
Aug 28, 2009, 12:51:59 PM8/28/09
to nhusers
well, i think, in my imho, this almost pure business logic but it
depends on data acces code, so, it could be good to redefine preinsert
nh events, and do that logic inside there maybe
just an option

Tuna Toksoz

unread,
Aug 28, 2009, 2:03:30 PM8/28/09
to nhu...@googlegroups.com
you can use NH event listeners, which I believe is all you need.

Tuna Toksöz
Eternal sunshine of the open source mind.

http://devlicio.us/blogs/tuna_toksoz
http://tunatoksoz.com
http://twitter.com/tehlike

Fabio Maulo

unread,
Aug 28, 2009, 5:28:10 PM8/28/09
to nhu...@googlegroups.com
Is it for Italian invoice numbers ?
even for "bolle accompagnatorie" ?

2009/8/28 Mauro Servienti <mauro.s...@gmail.com>



--
Fabio Maulo

Mauro Servienti

unread,
Aug 29, 2009, 6:40:47 AM8/29/09
to nhu...@googlegroups.com

Hi Tuna,

 

so you suggest to, for example, inherit from DefaultSaveEventListener and override the PerformSaveOrUpdate and here do what I need to do.

 

Is it correct?

 

.m

Mauro Servienti

unread,
Aug 29, 2009, 6:44:18 AM8/29/09
to nhu...@googlegroups.com
Hi Raul,

yes! That's why I am looking for a way to automate it, it's a border line
situation it's business logic but it is strictly linked to the storage.

.m

-----Original Message-----
From: nhu...@googlegroups.com [mailto:nhu...@googlegroups.com] On Behalf
Of Raul Carlomagno
Sent: venerdì 28 agosto 2009 18:52
To: nhusers
Subject: [nhusers] Re: Document number generation


Mauro Servienti

unread,
Aug 29, 2009, 6:49:46 AM8/29/09
to nhu...@googlegroups.com

Hi Fabio,

 

Yes, you get it, but not only. We have several document types, other then invoices and “bolle” such as customer account statements or a document protocol system (I don’t know how to call in English in Italian is “protocollo”) and every “document” type has its own id generation logic.

 

.m

 

From: nhu...@googlegroups.com [mailto:nhu...@googlegroups.com] On Behalf Of Fabio Maulo
Sent: venerdì 28 agosto 2009 23:28
To: nhu...@googlegroups.com
Subject: [nhusers] Re: Document number generation

 

Is it for Italian invoice numbers ?

Tuna Toksoz

unread,
Aug 29, 2009, 7:16:29 AM8/29/09
to nhu...@googlegroups.com
Yes Mauro.Doesn't have to be the default one, interface should work too.

Carlos Fortes

unread,
Aug 29, 2009, 7:16:53 AM8/29/09
to nhusers
I asked for this before and i was pointed to NH-1852.

Fabio Maulo

unread,
Aug 29, 2009, 9:06:25 AM8/29/09
to nhu...@googlegroups.com
Because that is a business resposibility and it is about some Italian business I have some difficult to explain you how you can do it in english.
Can you send the request to the Italian forum ?
When you will do it, please add some info. about the usecase (when and if you want show the number of the document).

2009/8/29 Mauro Servienti <mauro.s...@gmail.com>



--
Fabio Maulo

Mauro Servienti

unread,
Aug 29, 2009, 11:52:01 AM8/29/09
to nhusers
Hi Fabio,

On Aug 29, 3:06 pm, Fabio Maulo <fabioma...@gmail.com> wrote:
> Because that is a business resposibility and it is about some Italian
> business I have some difficult to explain you how you can do it in english.Can
> you send the request to the Italian forum ?http://groups.google.com/group/nh-it

done: http://groups.google.com/group/nh-it/browse_frm/thread/ed8a100f8a4b419c#

> When you will do it, please add some info. about the usecase (when and if
> you want show the number of the document).

.m
Reply all
Reply to author
Forward
0 new messages