views:

1408

answers:

1

I am wondering how to 'seed' an auto incrementing primary key value using Castle AR? For Example wanting the Orders table primary keys to start out as 10000. Is this something that is 1. possible 2. a good solution for creating order numbers?

Maybe there is a way to have consecutive auto incrementing field on the DB that is NOT the pk, seeded to 10000?

+2  A: 

Castle ActiveRecord is built on top of NHibernate and features of AR heavily rely on features of NHibernate. NHibernate contains several primary key generators:
1. native - This is the default generator. If you specify this then NHibernate automatically chooses generator type based on underlying database. For example, if I would have used native instead of identity in the above mapping snippet you will still get the same SQL because NHibernate is smart enough to understand that the underlying database SQL Server and it supports identity columns. NHibernate converts the returned values using Convert.ChangeType method.
2. identity - This can be used with Identity columns provided with SQL Server, MySQL, Sybase etc.,
3. sequence - Firebird, DB2, PostgreSQL, Oracle, SAP DB supports sequences
4. increment - This generator does not uses any database feature like sequence or identity. NHibernate automatically increments 1 to last primary key value. This generator is helpful when dealing with single database system but it does not help in cluster based environment.
5. hilo - Hi/Lo algorithm is used to generate primary key values. This is very efficient when compared to other generator types. When used, NHibernate creates a separate table named hibernate_unique_key and creates a column named next_hi and then NHibernate uses this table as a reference when INSERT happens. We will talk elaborately on this later in this post.
6. uuid.hex - Uses System.Guid and its ToString method for generating string based primary key values.
7. guid - This can be used when the class property type is Guid.
8. guid.comb - This is similar as guid but uses a different algorithm to produce primary key values. Note that uuid.hex, guid, guid.comb uses UNIQUEIDENTIFIER as a column data type in SQL Server.
9. assigned - last but not least, this generator assumes that the primary key value is assigned by the user.

So you can see that there are no such build-in functionality. In order to create an order number you can use 2 ways:
1. select max order and manually set it
2. add some insert trigger to database
In my opinion you should use first way because in this way you will not rely on database. And you can reuse this functionality when you will need to move an object up or down. I'm usually using this way.

zihotki